Gan*_*alf 1 python-3.x python-asyncio
我有这个程序
import time
import asyncio
def is_prime(x):
return not any(x//i == x/i for i in range(x-1, 1, -1))
async def highest_prime_below(x):
print('Highest prime below %d' % x)
for y in range(x-1, 0, -1):
if is_prime(y):
print('? Highest prime below %d is %d' % (x, y))
return y
await asyncio.sleep(0.01)
return None
async def main():
t0 = time.time()
await asyncio.wait( [
highest_prime_below(100000),
highest_prime_below(10000),
highest_prime_below(1000)
] )
t1 = time.time()
print('Took %.2f ms' % (1000*(t1-t0)))
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
#loop.close()
Run Code Online (Sandbox Code Playgroud)
发现素数。你也可以在这里看到它https://osf.io/w8u26/
我的问题是,假设我有太多的函数调用
await asyncio.wait( [
highest_prime_below(100000),
highest_prime_below(10000),
highest_prime_below(1000)
#100 function calls here
] )
Run Code Online (Sandbox Code Playgroud)
我只有异步睡了这么多 await asyncio.sleep(0.01)
必须在设定的时间内完成所有功能,还是会在睡眠时间结束后取消某些功能?。
| 归档时间: |
|
| 查看次数: |
5280 次 |
| 最近记录: |