相关文章推荐
欢乐的柳树  ·  基于SuperMap iObjects ...·  4 天前    · 
欢乐的柳树  ·  WPF TabControl Page ...·  4 月前    · 
欢乐的柳树  ·  Python ...·  7 月前    · 
欢乐的柳树  ·  工作区库 - Azure ...·  9 月前    · 
欢乐的柳树  ·  Java泛型 | 击水湘江·  10 月前    · 
欢乐的柳树  ·  什么?TS ...·  10 月前    · 
欢乐的柳树  ·  MYSQL 8 show ...·  10 月前    · 
聪明的作业本  ·  Advanced query ...·  40 分钟前    · 
失望的鸡蛋面  ·  "Microsoft Outlook ...·  42 分钟前    · 
坚强的柿子  ·  mongodb 多表关联处理 : ...·  45 分钟前    · 
不爱学习的火腿肠  ·  java ...·  2 小时前    · 
旅行中的铁链  ·  错误信息:SSL ShakeHand ...·  2 小时前    · 
憨厚的金鱼  ·  Scanpy数据结构:AnnData - 何帅 ·  2 小时前    · 

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 代码,以提交一个任务给另一个线程中运行的事件循环。

 
推荐文章