quamash
quamash copied to clipboard
call asyncio.sleep(0.1) RuntimeError: no running event loop
Hello, I have been using quamash with pyqt5 for 2 years, it works great(thanks alot to you).
But recently my envirioment updated, I got error when calling asyncio.sleep(0.1)
in pyqt views bisiness.
My environment:
- linux system with kernel 4.19
- python3.8
- Quamash ==0.6.1
Here is some test code could raise the same error as my business:
import sys
import asyncio
import quamash
from PyQt5 import QtWidgets
async def loop_func():
for i in range(1000):
print('hello')
await asyncio.sleep(0.1)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
loop = quamash.QEventLoop(app)
asyncio.set_event_loop(loop)
with loop:
asyncio.ensure_future(loop_func(), loop=loop)
loop.run_forever()
Output:
Task exception was never retrieved
future: <Task finished name='Task-1' coro=<loop_func() done, defined at /home/wood/project/data-manage-client/test.py:7> exception=RuntimeError('no running event loop')>
Traceback (most recent call last):
File "/home/wood/project/data-manage-client/test.py", line 10, in loop_func
await asyncio.sleep(0.1)
File "/usr/lib64/python3.8/asyncio/tasks.py", line 637, in sleep
loop = events.get_running_loop()
RuntimeError: no running event loop
I have search the same error: https://github.com/gmarull/asyncqt/issues/12 https://github.com/gmarull/asyncqt/pull/13
Refer to that, I solve it with:
asyncio.set_event_loop(loop)
asyncio.events._set_running_loop(loop) # <--------------------------------------------
with loop:
....