A
A
Alikhan2021-12-13 10:53:07
Python
Alikhan, 2021-12-13 10:53:07

RuntimeError: There is no current event loop in thread 'Thread-2'. What to do?

I decided to use multithreading in my bot written in Python using the Aiogram module.

examination_thread = Thread(target=examination)
start_polling_thread = Thread(target=executor.start_polling, args=(dp,))

I created two threads, one calls my function, and the second one calls the executor.start_polling function to start the bot.
(Before that, everything needed was imported).

But it gives me this error:
Traceback (most recent call last):
  File "C:\Users\123\AppData\Local\Programs\Python\Python38\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "C:\Users\123\AppData\Local\Programs\Python\Python38\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
C:\Users\123\AppData\Local\Programs\Python\Python38\lib\threading.py:870: RuntimeWarning: coroutine 'examination' was never awaited 
  self._target(*self._args, **self._kwargs)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
  File "C:\Users\123\AppData\Local\Programs\Python\Python38\lib\site-packages\aiogram\utils\executor.py", line 45, in start_polling 
    executor.start_polling(
  File "C:\Users\123\AppData\Local\Programs\Python\Python38\lib\site-packages\aiogram\utils\executor.py", line 317, in start_polling
    loop = asyncio.get_event_loop()
  File "C:\Users\123\AppData\Local\Programs\Python\Python38\lib\asyncio\events.py", line 639, in get_event_loop
    raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError: There is no current event loop in thread 'Thread-2'.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-12-13
@Vindicar

Mixing multithreading with asynchrony is a bad idea. Why did you need it?
If you need to execute a long-running synchronous task on a separate thread, use loop.run_in_executor() - it allows you to neatly represent the execution of a thread as a normal asynchronous task.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question