CabbageDevelopment / qasync

Python library for using asyncio in Qt-based applications.
BSD 2-Clause "Simplified" License
321 stars 45 forks source link

NotImplementedError with nest_asyncio on Unix #108

Open pkgw opened 9 months ago

pkgw commented 9 months ago

I'm experiencing a crash when I try to combine qasync with nest_asyncio on a Linux machine (running Python 3.9, Qt 5.15, but the problem doesn't seem to depend on those factors).

Here's a reproduction case:

#! /usr/bin/env python3

import asyncio
import nest_asyncio
from qasync import QEventLoop, QApplication

async def inner():
    print("inner before")
    await asyncio.sleep(1)
    print("inner after")

async def main():
    nest_asyncio.apply()
    loop = asyncio.get_event_loop()
    loop.run_until_complete(inner())
    await asyncio.sleep(1)  # justify this function's async-ness

app = QApplication([])
event_loop = QEventLoop(app)
asyncio.set_event_loop(event_loop)

with event_loop:
    event_loop.run_until_complete(main())

This leads to:

Traceback (most recent call last):
  File "./crashdemo.py", line 32, in <module>
    test()
  File "./crashdemo.py", line 26, in test
    loop.run_until_complete(inner())
  File "/a/lib/python3.9/site-packages/nest_asyncio.py", line 93, in run_until_complete
    self._run_once()
  File "/a/lib/python3.9/site-packages/nest_asyncio.py", line 116, in _run_once
    event_list = self._selector.select(timeout)
  File "/a/lib/python3.9/site-packages/qasync/_unix.py", line 53, in select
    raise NotImplementedError

Am I doing something wrong here? It's been a while since I've worked on the code that uses this functionality, but I think that this worked in my application when I first wrote the code, which was about 2 years ago.

addendum: I did just notice that the nest-asyncio README says that "[event] loops from other projects, such as uvloop or quamash, generally can’t be patched.", so maybe this is something that shouldn't be expected to work. I don't know enough about the codebase to know whether the above error is indicative of this, or something that can potentially be fixed.