添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Python Asyncio使用asyncio.run_coroutine_threadsafe不能运行新的coroutine

1 人关注

1>Python Asyncio is not running new coroutine using asyncio.run_coroutine_threadsafe. Below is the code testing performed on Mac.
————————————————————————————————

import os    
import random      
import asyncio      
import logging     
from concurrent.futures import ThreadPoolExecutor      
os.environ['PYTHONASYNCIODEBUG'] = '1'      
logging.basicConfig(level=logging.WARNING)      
async def coro():      
    print("Coroutine {} is has started")      
async def main(loop):     
    print(" 1 ")    
    fut = asyncio.run_coroutine_threadsafe(coro(), loop)      
    print(f"Future  --")          
    print(" 2 ")      
    print(" Result ",fut.result())      
    print(" 3 ")      
if __name__== '__main__':      
    loop = asyncio.get_event_loop()      
    loop.set_debug(True)      
    loop.run_until_complete(main(loop))      

————————————————————————————————
Output:

Future -- <Future at 0x102a05358 state=pending>

=============================================================================

2>The output is same when I run on a different executor also as shown below
—————————————————————————————————————

new_loop = asyncio.new_event_loop()      
new_loop.set_default_executor(ThreadPoolExecutor(max_workers=2))      
fut = asyncio.run_coroutine_threadsafe(coro(), new_loop) 

—————————————————————————————————————
样品代码。

import os      
import random      
import asyncio      
import logging      
from concurrent.futures import ThreadPoolExecutor      
os.environ['PYTHONASYNCIODEBUG'] = '1'      
logging.basicConfig(level=logging.WARNING)      
async def coro():      
    print("Coroutine {} is has started")      
async def main(loop):    
    print(" 1 ")    
    new_loop = asyncio.new_event_loop()    
    new_loop.set_default_executor(ThreadPoolExecutor(max_workers=2))    
    fut = asyncio.run_coroutine_threadsafe(coro(), new_loop)      
    print(f"Future  --")        
    print(" 2 ")      
    print(" Result ",fut.result())      
    print(" 3 ")      
if __name__== '__main__':      
    loop = asyncio.get_event_loop()    
    loop.set_debug(True)      
    loop.run_until_complete(main(loop))    

————————————————————————————————
Output:

Future -- <Future at 0x102f5d668 state=pending>
python
python-asyncio
partha1984
partha1984
发布于 2019-12-22
2 个回答
RafalS
RafalS
发布于 2019-12-22
0 人赞同

From asyncio.run_coroutine_threadsafe :

这个函数应该从不同的操作系统线程中调用,而不是在事件循环中运行的线程。

async def coro():
    print("Coroutine {} is has started")
def start_coro(loop):
    fut = asyncio.run_coroutine_threadsafe(coro(), loop)
    print(fut.result())
if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.set_debug(True)
    Thread(target=start_coro, args=(loop,)).start()
    loop.run_forever()
    
user4815162342
user4815162342
发布于 2019-12-22
0 人赞同

第一个代码段将一个coroutine提交到它已经运行的事件循环中。你不需要 run_coroutine_threadsafe ,你可以直接调用 asyncio.create_task 。正如在另一个答案中提到的, run_coroutine_threadsafe 的想法是,你从你的 sync 代码,以提交一个任务给另一个线程中运行的事件循环。