|
4 | 4 | import logging |
5 | 5 | import uuid |
6 | 6 | import sys |
| 7 | +import asyncio |
7 | 8 |
|
8 | 9 | from concurrent import futures |
9 | 10 | from .exceptions import (JsonRpcException, JsonRpcRequestCancelled, |
@@ -35,6 +36,17 @@ def __init__(self, dispatcher, consumer, id_generator=lambda: str(uuid.uuid4()), |
35 | 36 | self._client_request_futures = {} |
36 | 37 | self._server_request_futures = {} |
37 | 38 | self._executor_service = futures.ThreadPoolExecutor(max_workers=max_workers) |
| 39 | + self._cancelledRequests = set() |
| 40 | + self._messageQueue = asyncio.Queue() |
| 41 | + |
| 42 | + async def consume_task(self): |
| 43 | + log.warning("starting task") |
| 44 | + loop = asyncio.get_running_loop() |
| 45 | + while True: |
| 46 | + message = await self._messageQueue.get() |
| 47 | + await loop.run_in_executor(None, self.consume, message) |
| 48 | + log.warning("got message in task") |
| 49 | + self._messageQueue.task_done() |
38 | 50 |
|
39 | 51 | def shutdown(self): |
40 | 52 | self._executor_service.shutdown() |
@@ -94,7 +106,15 @@ def callback(future): |
94 | 106 | future.set_exception(JsonRpcRequestCancelled()) |
95 | 107 | return callback |
96 | 108 |
|
| 109 | + async def consume_async(self, message): |
| 110 | + log.warning("got message put in queue") |
| 111 | + if message['method'] == CANCEL_METHOD: |
| 112 | + self._cancelledRequests.add(message.get('params')['id']) |
| 113 | + await self._messageQueue.put(message) |
| 114 | + |
| 115 | + |
97 | 116 | def consume(self, message): |
| 117 | + log.warning("consume message") |
98 | 118 | """Consume a JSON RPC message from the client. |
99 | 119 |
|
100 | 120 | Args: |
@@ -182,6 +202,9 @@ def _handle_request(self, msg_id, method, params): |
182 | 202 | except KeyError as e: |
183 | 203 | raise JsonRpcMethodNotFound.of(method) from e |
184 | 204 |
|
| 205 | + if msg_id in self._cancelledRequests: |
| 206 | + raise JsonRpcRequestCancelled() |
| 207 | + |
185 | 208 | handler_result = handler(params) |
186 | 209 |
|
187 | 210 | if callable(handler_result): |
|
0 commit comments