Runtime error: Event loop is running

后端 未结 1 1174
旧时难觅i
旧时难觅i 2021-02-07 15:33

I get the following error when I call the function send_message.

Exception in thread Thread-1:
Traceback (most recent call last):
  File \"/usr/lib/         


        
1条回答
  •  孤街浪徒
    2021-02-07 15:53

    The "Event loop is running" exception indicates you called loop.run_until_complete on a loop that is already running, perhaps in another thread.

    1. If the loop is already running in another thread and you want to submit a coroutine for it to execute, use:

      asyncio.run_coroutine_threadsafe(client.send_message(SERVER, message), client.loop)
      
    2. If you are trying to add a coroutine to the loop and that loop is running on the current thread, then the best way is probably to just await/yield from it

    3. If you're scheduling it from a synchronous function, then you probably want:

      asyncio.ensure_future(
          client.send_message(SERVER, message),
          loop=client.loop
      ).add_done_callback(fn)
      

      Where fn is a function whose only parameter is the future that is created by ensure_future and which is called after the future is completed.

    0 讨论(0)
提交回复
热议问题