Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDK-3862] Add sync support using decorator #535

Closed
wants to merge 22 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
closed eventloop thread properly as a part of close method
sacOO7 committed Sep 29, 2023
commit 55a773fca45cd1530fc1ed7c7f3a27c20b08d341
11 changes: 6 additions & 5 deletions ably/executer/eventloop.py
Original file line number Diff line number Diff line change
@@ -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(
@@ -29,7 +30,7 @@ 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
@@ -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
@@ -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()

4 changes: 2 additions & 2 deletions ably/rest/rest.py
Original file line number Diff line number Diff line change
@@ -154,10 +154,10 @@ 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()