Skip to content

Commit 187ee78

Browse files
committed
Optimize unsubscribing to ops
1 parent fe91dbb commit 187ee78

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

graphql_ws/django/subscriptions.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,20 @@ async def run_op(self, connection_context, op_id, iterator):
8383
await self.on_operation_complete(connection_context, op_id)
8484

8585
async def on_close(self, connection_context):
86-
remove_operations = list(connection_context.operations.keys())
87-
cancelled_tasks = []
88-
for op_id in remove_operations:
89-
task = await self.unsubscribe(connection_context, op_id)
90-
if task:
91-
cancelled_tasks.append(task)
86+
# Unsubscribe from all the connection's current operations in parallel.
87+
unsubscribes = [
88+
self.unsubscribe(connection_context, op_id)
89+
for op_id in connection_context.operations
90+
]
91+
cancelled_tasks = [task for task in await asyncio.gather(*unsubscribes) if task]
9292
# Wait around for all the tasks to actually cancel.
93-
await asyncio.wait(cancelled_tasks)
93+
if cancelled_tasks:
94+
await asyncio.wait(cancelled_tasks)
9495

9596
async def on_stop(self, connection_context, op_id):
9697
task = await self.unsubscribe(connection_context, op_id)
97-
await asyncio.wait([task])
98+
if task:
99+
await asyncio.wait([task])
98100

99101
async def unsubscribe(self, connection_context, op_id):
100102
op = None

0 commit comments

Comments
 (0)