Skip to content

Commit

Permalink
Send events asynchronously.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaul Kremer committed Aug 23, 2023
1 parent aee8c0d commit e8186e7
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions fastapi_websocket_pubsub/event_broadcaster.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ def __init__(
# If we opt to manage the context directly (i.e. call async with on the event broadcaster itself)
self._context_manager = None
self._context_manager_lock = asyncio.Lock()
self._tasks = set()

async def __broadcast_notifications__(self, subscription: Subscription, data):
"""
Expand Down Expand Up @@ -267,11 +268,18 @@ async def __read_notifications__(self):
)
)
# Notify subscribers of message received from broadcast
await self._notifier.notify(
notification.topics,
notification.data,
notifier_id=self._id,
task = asyncio.create_task(
self._notifier.notify(
notification.topics,
notification.data,
notifier_id=self._id,
)
)

self._tasks.add(task)
def cleanup(task):
self._tasks.remove(task)
task.add_done_callback(cleanup)
except:
logger.exception("Failed handling incoming broadcast")
logger.info(
Expand Down

0 comments on commit e8186e7

Please sign in to comment.