self asyncio

时间:2023-03-10 03:36:28
self asyncio
import asyncio
from threading import Thread
import time print('main start:',time.time()) async def download(i):
print(i, time.time())
await asyncio.sleep(i)
b=linkextractor(i)
if b is not None:
await asyncio.wait(b)
else:
print('all complete',time.time())
return True def linkextractor(content):
if content<3:
tasks=[]
tasks.append(download(content+1))
tasks.append(download(content+1))
tasks.append(download(content+1))
return tasks
print('end linkextractor', time.time()) def start_loop(loop):
asyncio.set_event_loop(loop)
loop.run_forever() new_loop=asyncio.new_event_loop()
t=Thread(target=start_loop,args=(new_loop,))
t.start() asyncio.run_coroutine_threadsafe(download(1),new_loop)