fix: Create new event loop for cleanup if current one is not running

This commit is contained in:
Mike A.
2024-08-02 13:07:31 +02:00
parent d92085d621
commit b7ba97f30c

View File

@@ -29,6 +29,9 @@ class Closable(ABC):
"""Attempt to automatically clean up when garbage collected."""
try:
loop = self._loop or asyncio.get_running_loop()
loop.call_soon_threadsafe(loop.create_task, self.close())
if loop.is_running():
loop.call_soon_threadsafe(loop.create_task, self.close())
else:
loop.run_until_complete(self.close())
except RuntimeError:
pass