Issue
I want to create a task with asyncio.create_task, which must run in the background without blocking currently running function. However, when I attempt to create it, I get an error, saying that coroutine was never awaited.
I tried to just call create_task() with a coroutine without awaiting it first, thinking that it would run after I created the task:
async def _turn_end_timer(self):
await asyncio.sleep(self.turn_time)
self.next_turn()
def next_turn(self):
if self._turn_worker is not None:
self._turn_worker.cancel()
if len(self.turn_queue) == 0:
self.current_turn_username = None
return
self.current_turn_username = self.turn_queue.pop(0)
coro = self._turn_end_timer()
self._turn_worker = asyncio.create_task(coro)
self.send_turn_queue_update()
That did not work. The event loop is running:
asyncio.get_event_loop().run_until_complete(main())
asyncio.get_event_loop().run_forever()
What does that mean and how to fix it? I do not want to use threading
module, since it would cause more trouble.
Solution
asyncio would not allow multiple event_loop to run but if you need a by pass that is you are getting the event_loop already running then you can use this library
import nest_asyncio
nest_asyncio.apply()
This will allow for a multi threaded behaviour
Answered By - chukwuka mark
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.