Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do sys.exit() with asyncio Python 3.8+/Windows 10+ without exceptions?

I want to write such asyncio program which exists on on some problems. I should work but it generates ugly output instead of clean exit. How to do sys.exit() without such ugly exceptions?

Here is code:

import asyncio
import sys


async def main():
    sys.exit()


if __name__ == '__main__':
    asyncio.run(main(), debug=True)

Result is:

C:\root\Python38\python.exe C:/Users/x/PycharmProjects/dptr-monitoring-v2/sandbox/python/s06_asyncio/s02_exit.py
Cancelling an overlapped future failed
future: <_OverlappedFuture pending overlapped=<pending, 0x1dba46c58b0> cb=[BaseProactorEventLoop._loop_self_reading()] created at C:\root\Python38\lib\asyncio\windows_events.py:461>
source_traceback: Object created at (most recent call last):
  File "C:/Users/x/PycharmProjects/dptr-monitoring-v2/sandbox/python/s06_asyncio/s02_exit.py", line 10, in <module>
    asyncio.run(main(), debug=True)
  File "C:\root\Python38\lib\asyncio\runners.py", line 47, in run
    loop.run_until_complete(loop.shutdown_asyncgens())
  File "C:\root\Python38\lib\asyncio\base_events.py", line 599, in run_until_complete
    self.run_forever()
  File "C:\root\Python38\lib\asyncio\windows_events.py", line 316, in run_forever
    super().run_forever()
  File "C:\root\Python38\lib\asyncio\base_events.py", line 567, in run_forever
    self._run_once()
  File "C:\root\Python38\lib\asyncio\base_events.py", line 1847, in _run_once
    handle._run()
  File "C:\root\Python38\lib\asyncio\events.py", line 81, in _run
    self._context.run(self._callback, *self._args)
  File "C:\root\Python38\lib\asyncio\proactor_events.py", line 769, in _loop_self_reading
    f = self._proactor.recv(self._ssock, 4096)
  File "C:\root\Python38\lib\asyncio\windows_events.py", line 461, in recv
    return self._register(ov, conn, finish_recv)
Traceback (most recent call last):
  File "C:\root\Python38\lib\asyncio\runners.py", line 43, in run
    return loop.run_until_complete(main)
  File "C:\root\Python38\lib\asyncio\base_events.py", line 599, in run_until_complete
    self.run_forever()
  File "C:\root\Python38\lib\asyncio\windows_events.py", line 316, in run_forever
    super().run_forever()
  File "C:\root\Python38\lib\asyncio\base_events.py", line 567, in run_forever
    self._run_once()
  File "C:\root\Python38\lib\asyncio\base_events.py", line 1847, in _run_once
    handle._run()
  File "C:\root\Python38\lib\asyncio\events.py", line 81, in _run
    self._context.run(self._callback, *self._args)
  File "C:/Users/x/PycharmProjects/dptr-monitoring-v2/sandbox/python/s06_asyncio/s02_exit.py", line 6, in main
    exit()
  File "C:\root\Python38\lib\_sitebuiltins.py", line 26, in __call__
    raise SystemExit(code)
SystemExit: None

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\root\Python38\lib\asyncio\windows_events.py", line 66, in _cancel_overlapped
    self._ov.cancel()
OSError: [WinError 6] Nieprawidłowe dojście

Process finished with exit code 0

Should be:

Process finished with exit code 0

like image 825
Chameleon Avatar asked Sep 03 '25 10:09

Chameleon


1 Answers

Very late response but I just had the same problem (with argparse as in the comments above). Not sure whether this is an acceptable solution but I solved it by awaiting in the main() routine as follows:

import asyncio
import sys


async def main():
    await asyncio.sleep(0)
    sys.exit()


if __name__ == '__main__':
    asyncio.run(main(), debug=True)
like image 151
Phil Avatar answered Sep 05 '25 00:09

Phil