File tree Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Original file line number Diff line number Diff line change @@ -87,6 +87,30 @@ terminates, after canceling the other task::
87
87
for task in pending:
88
88
task.cancel()
89
89
90
+ In-process queue
91
+ ----------------
92
+
93
+ For simple applications that need to be able to consume and act on multiple
94
+ messages at one, an in-process queue can be leveraged::
95
+
96
+ queue = asyncio.Queue()
97
+
98
+ async def consumer_handler(websocket):
99
+ for message in websocket:
100
+ queue.put(message)
101
+
102
+ async def worker():
103
+ while True:
104
+ message = await queue.get()
105
+ result = await long_running_task(message)
106
+ await websocket.send(result)
107
+
108
+ async def handler(websocket):
109
+ async with asyncio.TaskGroup() as tg:
110
+ tg.create_task(consumer_handler(websocket))
111
+ for _ in range(os.cpu_count()):
112
+ tg.create_task(worker())
113
+
90
114
Registration
91
115
------------
92
116
You can’t perform that action at this time.
0 commit comments