asyncio动态添加任务
asyncio.run_forever()下动态添加任务
方法一、asyncio.run_coroutine_threadsafe(coroutine, loop)
方法二、asyncio.call_soon_threadsafe(callback, *args, context=None)
方法一、实例run_coroutine_threadsafe
通过添加新的coroutin对象,放入loop内,动态添加。(爬虫正解)
import asyncio import time from threading import Thread def start_loop(loop): asyncio.set_event_loop(loop) print("start loop", time.time()) loop.run_forever() async def do_some_work(x): print('start {}'.format(x)) await asyncio.sleep(x) print('Done after {}s'.format(x)) new_loop = asyncio.new_event_loop() t = Thread(target=start_loop, args=(new_loop,)) t.start() asyncio.run_coroutine_threadsafe(do_some_work(6), new_loop) asyncio.run_coroutine_threadsafe(do_some_work(4), new_loop)
方法二、实例call_soon_threadsafe
如果是io操作,就会相当于变成线程去阻塞执行添加进去的函数,密集型则没关系。
import asyncio import time from threading import Thread def start_loop(loop): asyncio.set_event_loop(loop) print("start loop") loop.run_forever() def more_work(x): print('start work {}'.format(x)) time.sleep(x) print('Finished work {}'.format(x)) new_loop = asyncio.new_event_loop() t = Thread(target=start_loop, args=(new_loop,)) t.start() new_loop.call_soon_threadsafe(more_work, 6) new_loop.call_soon_threadsafe(more_work, 3)