-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
anyio integration #1157
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
anyio integration #1157
Changes from 4 commits
d06f40c
75310b5
a660684
42b83cb
9870a1f
6997eb9
e1c2adb
de84b4a
e91ec33
7e2cd46
03e312e
fd4569e
d785513
58d5331
268547d
57b2f79
444a3ac
4d31a60
681c348
01dd813
9f76d42
5c8818d
f0e4cd8
376f9db
34da2b4
31cc220
73590aa
4192bf7
3a4b472
cc3be48
9b6e722
b8c43cf
d51d5ff
82431f4
2504237
87c614c
cf915bc
72586ba
1800f7a
89e2dae
f62a2ec
edba5dc
60d95e1
fc60420
27283aa
3cce6a9
cbc2e68
c4d49a7
4dd8c5d
dde5079
df53965
6e0f05f
3a359e3
5c77b7d
390b7a1
e420181
6a3f94d
0c225a3
5667a4b
19685db
4e43146
a1ceb35
63cfcb9
efbe6a1
6208ca5
8e6115b
e0c9967
27ec6f7
2b9dd22
643d107
d0ca3f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| import typing | ||
|
|
||
| import anyio | ||
|
|
||
| from starlette.datastructures import State, URLPath | ||
| from starlette.exceptions import ExceptionMiddleware | ||
| from starlette.middleware import Middleware | ||
|
|
@@ -109,7 +111,9 @@ def url_path_for(self, name: str, **path_params: str) -> URLPath: | |
|
|
||
| async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: | ||
| scope["app"] = self | ||
| await self.middleware_stack(scope, receive, send) | ||
| task_group = scope["task_group"] = anyio.create_task_group() | ||
|
||
| async with task_group: | ||
| await self.middleware_stack(scope, receive, send) | ||
|
||
|
|
||
| # The following usages are now discouraged in favour of configuration | ||
| # during Starlette.__init__(...) | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,8 +1,9 @@ | ||||||
| import asyncio | ||||||
| import io | ||||||
| import sys | ||||||
| import typing | ||||||
|
|
||||||
| import anyio | ||||||
|
|
||||||
| from starlette.concurrency import run_in_threadpool | ||||||
| from starlette.types import Message, Receive, Scope, Send | ||||||
|
|
||||||
|
|
@@ -69,9 +70,8 @@ def __init__(self, app: typing.Callable, scope: Scope) -> None: | |||||
| self.scope = scope | ||||||
| self.status = None | ||||||
| self.response_headers = None | ||||||
| self.send_event = asyncio.Event() | ||||||
| self.send_event = anyio.create_event() | ||||||
| self.send_queue = [] # type: typing.List[typing.Optional[Message]] | ||||||
| self.loop = asyncio.get_event_loop() | ||||||
| self.response_started = False | ||||||
| self.exc_info = None # type: typing.Any | ||||||
|
|
||||||
|
|
@@ -83,31 +83,37 @@ async def __call__(self, receive: Receive, send: Send) -> None: | |||||
| body += message.get("body", b"") | ||||||
| more_body = message.get("more_body", False) | ||||||
| environ = build_environ(self.scope, body) | ||||||
| sender = None | ||||||
| try: | ||||||
| sender = self.loop.create_task(self.sender(send)) | ||||||
| await run_in_threadpool(self.wsgi, environ, self.start_response) | ||||||
| self.send_queue.append(None) | ||||||
| self.send_event.set() | ||||||
| await asyncio.wait_for(sender, None) | ||||||
| if self.exc_info is not None: | ||||||
| raise self.exc_info[0].with_traceback( | ||||||
| self.exc_info[1], self.exc_info[2] | ||||||
|
|
||||||
| async with anyio.create_task_group() as task_group: | ||||||
| sender_finished = anyio.create_event() | ||||||
| try: | ||||||
| await task_group.spawn(self.sender, send, sender_finished) | ||||||
| await anyio.run_sync_in_worker_thread( | ||||||
| self.wsgi, environ, self.start_response | ||||||
| ) | ||||||
| self.send_queue.append(None) | ||||||
| await self.send_event.set() | ||||||
| await sender_finished.wait() | ||||||
| if self.exc_info is not None: | ||||||
| raise self.exc_info[0].with_traceback( | ||||||
| self.exc_info[1], self.exc_info[2] | ||||||
| ) | ||||||
| finally: | ||||||
| await task_group.cancel_scope.cancel() | ||||||
|
||||||
| await task_group.cancel_scope.cancel() | |
| task_group.cancel_scope.cancel() |
Uh oh!
There was an error while loading. Please reload this page.