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

asyncio.gather 적용 #76

Merged
merged 1 commit into from
Dec 20, 2024
Merged
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
15 changes: 13 additions & 2 deletions conn/manager/internal/connection_manager.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
from fastapi.websockets import WebSocket
from conn import Conn
from message import Message
Expand Down Expand Up @@ -65,22 +66,32 @@ def generate_conn_id():
@staticmethod
async def receive_broadcast_event(message: Message):
overwrite_event(message)

coroutines = []

for id in ConnectionManager.conns:
conn = ConnectionManager.conns[id]
await conn.send(message)
coroutines.append(conn.send(message))

await asyncio.gather(*coroutines)

@EventBroker.add_receiver("multicast")
@staticmethod
async def receive_multicast_event(message: Message):
overwrite_event(message)
if "target_conns" not in message.header:
raise DumbHumanException()

coroutines = []

for conn_id in message.header["target_conns"]:
conn = ConnectionManager.get_conn(conn_id)
if not conn:
raise DumbHumanException()

await conn.send(message)
coroutines.append(conn.send(message))

await asyncio.gather(*coroutines)

@staticmethod
async def handle_message(message: Message):
Expand Down
7 changes: 6 additions & 1 deletion event/internal/event_broker.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import annotations
import asyncio
from typing import Callable, Generic
from message import Message
from .exceptions import NoMatchingReceiverException
Expand Down Expand Up @@ -78,10 +79,14 @@ async def publish(message: Message):
if message.event not in EventBroker.event_dict:
raise NoMatchingReceiverException(message.event)

coroutines = []

receiver_ids = EventBroker.event_dict[message.event]
for id in receiver_ids:
receiver = Receiver.get_receiver(id)
await receiver(message)
coroutines.append(receiver(message))

await asyncio.gather(*coroutines)

def _debug(message: Message):
print(message.to_str(del_header=False))
Loading