python 异步编程 asyncio
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 异步编程 asyncio相关的知识,希望对你有一定的参考价值。
import datetime import time import asyncio import numba as nb import numpy as np @nb.jit() def f(n): if n<2: return 1 else: return f(n-1)+f(n-2) async def fun(m): asyncio.sleep(0.1) return ‘输出{}‘.format(i) loop = asyncio.get_event_loop() st = time.time() for i in range(41000): s = loop.run_until_complete(fun(i)) # print(s,‘ ‘,time.strftime(‘%Y-%m-%d %H:%M:%S‘,time.localtime())) st2 = time.time() print(‘异步编程用时:‘,st2-st,‘ s‘) def fun2(m): time.sleep(0.1) return ‘输出{}‘.format(i) for i in range(41): s = fun2(i) # print(s, ‘ ‘, time.time()) print(‘同步编程用时:‘,time.time()-st2,‘ s‘)
以上是关于python 异步编程 asyncio的主要内容,如果未能解决你的问题,请参考以下文章