When the following code works (the maximum test and a small example is sketched), after the block_func method completes, the stream in which the method was executed does not end.
def block_func(): i = 1 async def starter(): future = asyncio.get_event_loop().run_in_executor( executor=None, func=functools.partial(block_func) ) while True: await asyncio.sleep(1) asyncio.ensure_future(starter()) loop = asyncio.get_event_loop() loop.run_forever() It does not help even the option of cleaning tasks:
def block_func(): i = 1 async def starter(): future = asyncio.get_event_loop().run_in_executor( executor=None, func=functools.partial(block_func) ) while True: await asyncio.sleep(1) async def cleaner(): while True: for task in asyncio.Task.all_tasks(): try: if task.exception() or task.done(): asyncio.Task._all_tasks.remove(task) except asyncio.futures.CancelledError: asyncio.Task._all_tasks.remove(task) except asyncio.futures.InvalidStateError: pass # :TODO: Чистим последний таск, иначе не возбуждаются ошибки из корутины task = None await asyncio.sleep(5) asyncio.ensure_future(starter()) asyncio.ensure_future(cleaner()) loop = asyncio.get_event_loop() loop.run_forever() I tried to substitute executor = concurrent.futures.ThreadPoolExecutor (), in this case it turns out to terminate the stream by calling the future result method to read the result of the block_func method in the stream.
But I don’t like this solution, I want to understand why the thread doesn’t end by itself when the execution of the method inside the stream ends.