python 异步调用

最近遇到个问题, NLP相关的代码,尤其是DL相关的代码,处理千万级数据显得会慢一些,有可能要好几个小时,那前端的等待是不可行的,这个时候就需要采用python的异步函数了。

同步代码

import time

def hello():
    time.sleep(1)

def run():
    for i in range(5):
        hello()
        print('Hello World:%s' % time.time())  
if __name__ == '__main__':
    run()

Hello World:1536842494.2786784
Hello World:1536842495.2796268
Hello World:1536842496.2802596
Hello World:1536842497.2804587
Hello World:1536842498.2812462

异步代码

import time
import asyncio

# 定义异步函数
async def hello():
    print('Hello World:%s' % time.time())
    #必须使用await,不能使用yield from;如果是使用yield from ,需要采用@asyncio.coroutine相对应
    await asyncio.sleep(1)   
    print('Hello wow World:%s' % time.time())

def run():
    tasks = []
    for i in range(5):
        tasks.append(hello())
    loop.run_until_complete(asyncio.wait(tasks))

loop = asyncio.get_event_loop()
if __name__ =='__main__':
    run()

Hello World:1536855050.1950748
Hello World:1536855050.1950748
Hello World:1536855050.1950748
Hello World:1536855050.1960726
Hello World:1536855050.1960726
(暂停约1秒)
Hello wow World:1536855051.1993241
Hello wow World:1536855051.1993241
Hello wow World:1536855051.1993241
Hello wow World:1536855051.1993241
Hello wow World:1536855051.1993241

具体可以参加以下链接:

https://blog.csdn.net/brucewong0516/article/details/82697935

https://www.ruanyifeng.com/blog/2019/11/python-asyncio.html

https://www.cnblogs.com/sui776265233/p/10950001.html

猜你喜欢

转载自blog.csdn.net/katrina1rani/article/details/109727036