Answer the question
In order to leave comments, you need to log in
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,))
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
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 questionAsk a Question
731 491 924 answers to any question