Skip to content

Commit

Permalink
closed eventloop thread properly as a part of close method
Browse files Browse the repository at this point in the history
  • Loading branch information
sacOO7 committed Sep 29, 2023
1 parent 1ccb72d commit e5fcea3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
12 changes: 7 additions & 5 deletions ably/executer/eventloop.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ def __init__(self):

@staticmethod
def get_global() -> 'AblyEventLoop':
if AblyEventLoop.__global_event_loop is None:
if (AblyEventLoop.__global_event_loop is None or
AblyEventLoop.__global_event_loop.loop.is_closed()):
AblyEventLoop.__global_event_loop = AblyEventLoop()
AblyEventLoop.__global_event_loop.__create_if_not_exist()
return AblyEventLoop.__global_event_loop

def __create_if_not_exist(self):
if self.loop is None:
if self.loop is None or self.loop.is_closed():
self.loop = asyncio.new_event_loop()
if not self.loop.is_running():
self.thread = threading.Thread(
Expand All @@ -29,7 +30,8 @@ def __create_if_not_exist(self):
self.thread.start()

def close(self) -> events:
self.loop.stop()
# https://stackoverflow.com/questions/46093238/python-asyncio-event-loop-does-not-seem-to-stop-when-stop-method-is-called
self.loop.call_soon_threadsafe(self.loop.stop)
self.thread.join()
self.loop.close()
self.loop = None
self.thread = None

2 changes: 2 additions & 0 deletions ably/http/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import httpx
import msgpack

from ably.decorator.sync import optional_sync
from ably.rest.auth import Auth
from ably.http.httputils import HttpUtils
from ably.transport.defaults import Defaults
Expand Down Expand Up @@ -131,6 +132,7 @@ def __init__(self, ably, options):
self.__host_expires = None
self.__client = httpx.AsyncClient(http2=True)

@optional_sync
async def close(self):
await self.__client.aclose()

Expand Down
5 changes: 3 additions & 2 deletions ably/rest/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,11 @@ async def __aexit__(self, *excinfo):
def __exit__(self, *excinfo):
self.close_sync()

@optional_sync
async def close(self):
await self.http.close()
AblyEventLoop.get_global().close()

def close_sync(self):
self.close()
self.http.close()
AblyEventLoop.get_global().close()

0 comments on commit e5fcea3

Please sign in to comment.