[nodriver] Using Nodriver in already running event loops #1978
-
Hi there, for some reason Nodriver should be initiated in the following way according to the documentation: if __name__ == "__main__":
# since asyncio.run never worked (for me)
# i use
uc.loop().run_until_complete(main()) However, this precludes using it in an already running event loop. Due to the complex structure of my app where Nodriver would serve a part as a scraper in background processes managed by Simple Asynchronous Queue (SAQ) I am using it in already running asynchronous loops. I can create a new thread to run Nodriver (theoretically) but that would be synchronous and I imagine it would cause issues on its own. I am therefore wondering if it is possible to run and close Nodriver from an already running loop without seeing the following generic exception raised every time when closing Nodriver: Exception ignored in: <function BaseSubprocessTransport.__del__ at 0x7fee26a2ccc0>
Traceback (most recent call last):
File "/usr/local/lib/python3.12/asyncio/base_subprocess.py", line 126, in __del__
File "/usr/local/lib/python3.12/asyncio/base_subprocess.py", line 104, in close
File "/usr/local/lib/python3.12/asyncio/unix_events.py", line 568, in close
File "/usr/local/lib/python3.12/asyncio/unix_events.py", line 592, in _close
File "/usr/local/lib/python3.12/asyncio/base_events.py", line 795, in call_soon
File "/usr/local/lib/python3.12/asyncio/base_events.py", line 541, in _check_closed
RuntimeError: Event loop is closed I have tried many various methods already, but always encounter this exception when Nodriver is closed. Are there any ways to bypass this shortcoming? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You can run it in whatever loop you want . To prevent the error you should close the websocket connections manually. Or, create task and run_until_complete like UC loop does but using your own loop. You could also use contextlib suppress to , well, suppress the exception |
Beta Was this translation helpful? Give feedback.
You can run it in whatever loop you want . To prevent the error you should close the websocket connections manually. Or, create task and run_until_complete like UC loop does but using your own loop.
You could also use contextlib suppress to , well, suppress the exception