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>