|
4 | 4 | import base64
|
5 | 5 | import logging
|
6 | 6 | from functools import wraps
|
7 |
| -from typing import Any, Awaitable, Callable, Dict, Optional, Text, TypeVar, Coroutine |
| 7 | +from typing import Any, Awaitable, Callable, Dict, Optional, Text, TypeVar, Coroutine, Set |
8 | 8 | import warnings
|
9 | 9 | import aiohttp
|
10 | 10 |
|
@@ -52,12 +52,22 @@ def create_recipient_id(reseller_token, project_token, dialog_id) -> Text:
|
52 | 52 | return base64.b64encode(bytes(json_representation, 'utf-8')).decode('utf-8')
|
53 | 53 |
|
54 | 54 |
|
| 55 | +class TaskContainer: |
| 56 | + tasks: Set[asyncio.Task] = set() |
| 57 | + |
| 58 | + def run(self, coro: Coroutine[Any, Any, None]): |
| 59 | + task = asyncio.create_task(coro) |
| 60 | + self.tasks.add(task) |
| 61 | + task.add_done_callback(self.tasks.discard) |
| 62 | + |
| 63 | + |
55 | 64 | class CVGOutput(OutputChannel):
|
56 | 65 | """Output channel for the Cognitive Voice Gateway"""
|
57 | 66 |
|
58 | 67 | on_message: Callable[[UserMessage], Awaitable[Any]]
|
59 | 68 | base_url: str
|
60 | 69 | proxy: Optional[str]
|
| 70 | + task_container: TaskContainer = TaskContainer() |
61 | 71 |
|
62 | 72 | @classmethod
|
63 | 73 | def name(cls) -> Text:
|
@@ -89,8 +99,7 @@ async def perform():
|
89 | 99 | status, body = await self._perform_request(path, method, data)
|
90 | 100 | await process_result(status, body)
|
91 | 101 |
|
92 |
| - # noinspection PyAsyncCall |
93 |
| - asyncio.create_task(perform()) |
| 102 | + self.task_container.run(perform()) |
94 | 103 |
|
95 | 104 | async def _say(self, dialog_id: str, text: str):
|
96 | 105 | await self._perform_request("/call/say", method="POST", data={DIALOG_ID_FIELD: dialog_id, "text": text})
|
@@ -214,6 +223,7 @@ class CVGInput(InputChannel):
|
214 | 223 | proxy: Optional[str]
|
215 | 224 | expected_authorization_header_value: str
|
216 | 225 | blocking_endpoints: bool
|
| 226 | + task_container: TaskContainer = TaskContainer() |
217 | 227 |
|
218 | 228 | @classmethod
|
219 | 229 | def name(cls) -> Text:
|
@@ -316,8 +326,7 @@ async def process_request(request: Request, text: Text, must_block: bool):
|
316 | 326 | if self.blocking_endpoints or must_block:
|
317 | 327 | await result
|
318 | 328 | else:
|
319 |
| - # noinspection PyAsyncCall |
320 |
| - asyncio.create_task(result) |
| 329 | + self.task_container.run(result) |
321 | 330 |
|
322 | 331 | return response.empty(204)
|
323 | 332 |
|
|
0 commit comments