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

Asynchronous API Alternatives #135

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 7 additions & 0 deletions realtime/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ def join(self) -> Channel:
loop.run_until_complete(self._join())
return self

async def join_async(self) -> None:
"""
Wrapper for async def _join().
:return: None
"""
await self._join()
Comment on lines +49 to +54
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (testing): Tests needed for join_async method.

The addition of join_async in Channel class also requires tests to ensure it integrates properly with the existing asynchronous infrastructure and behaves as expected under various conditions.


async def _join(self) -> None:
"""
Coroutine that attempts to join Phoenix Realtime server via a certain topic
Expand Down
20 changes: 19 additions & 1 deletion realtime/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ def listen(self) -> None:
loop = asyncio.get_event_loop() # TODO: replace with get_running_loop
loop.run_until_complete(asyncio.gather(self._listen(), self._keep_alive()))

async def listen_async(self) -> None:
"""
Wrapper for async def _listen() and async def _keep_alive() to expose an async interface.
:return: None
"""
# @ensure_connection is definitely nicer, but I don't know if it is also
# working for asynchronous functions.
if not self.connected:
raise NotConnectedError(self.listen_async.__name__)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (code_refinement): Consider adding a more descriptive error message.

Providing a more detailed error message can help in debugging and understanding the context of the error when it occurs.

Suggested change
raise NotConnectedError(self.listen_async.__name__)
raise NotConnectedError(f"Failed to connect using {self.listen_async.__name__} in realtime/connection.py. Ensure the connection is established before invoking this method.")

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have two questions regarding this:

  1. This basically the same exception and exception message as in the ensure_connection function.
  2. As written in my comment, the question is if should create also an ensure_connection_async function/decorator. Using simply ensure_connection was marked as an error in PyCharm.


Comment on lines +68 to +77
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (testing): Missing tests for the new asynchronous methods.

The PR introduces new asynchronous methods such as listen_async and connect_async, but there are no corresponding tests to verify their behavior. It's crucial to ensure these methods handle asynchronous operations correctly and maintain the expected state of the connection.

await asyncio.gather(self._listen(), self._keep_alive())

async def _listen(self) -> None:
"""
An infinite loop that keeps listening.
Expand Down Expand Up @@ -103,6 +115,12 @@ def connect(self) -> None:
loop.run_until_complete(self._connect())
self.connected = True

async def connect_async(self) -> None:
"""
Wrapper for async def _connect() to expose a async interface.
"""
await self._connect()

async def _connect(self) -> None:
ws_connection = await websockets.connect(self.url)

Expand Down Expand Up @@ -156,4 +174,4 @@ def summary(self) -> None:
"""
for topic, chans in self.channels.items():
for chan in chans:
print(f"Topic: {topic} | Events: {[e for e, _ in chan.callbacks]}]")
print(f"Topic: {topic} | Events: {[e for e, _ in chan.listeners]}")