asyncio.run_forever()下動態添加任務python
方法1、asyncio.run_coroutine_threadsafe(coroutine, loop)async
方法2、asyncio.call_soon_threadsafe(callback, *args, context=None)函數
方法1、實例run_coroutine_threadsafeoop
經過添加新的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)
方法2、實例call_soon_threadsafeorm
若是是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)