-
Notifications
You must be signed in to change notification settings - Fork 127
Add asyncio
backend
#930
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
Add asyncio
backend
#930
Changes from 7 commits
81b6b1f
828c491
b47b4c2
3814cf4
ac533ed
30f9c85
0038a0a
07772b1
ae741c0
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,228 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import asyncio | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import socket | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import ssl | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
from typing import Any, Dict, Iterable, Optional, Type | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
from .._exceptions import ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ConnectError, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ConnectTimeout, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ReadError, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ReadTimeout, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
WriteError, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
WriteTimeout, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
map_exceptions, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
from .._utils import is_socket_readable | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
from .base import SOCKET_OPTION, AsyncNetworkBackend, AsyncNetworkStream | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class AsyncIOStream(AsyncNetworkStream): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def __init__( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self, stream_reader: asyncio.StreamReader, stream_writer: asyncio.StreamWriter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self._stream_reader = stream_reader | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self._stream_writer = stream_writer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self._read_lock = asyncio.Lock() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self._write_lock = asyncio.Lock() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
self._read_lock = asyncio.Lock() | |
self._write_lock = asyncio.Lock() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
async with self._read_lock: | |
with map_exceptions(exc_map): | |
try: | |
return await asyncio.wait_for( | |
self._stream_reader.read(max_bytes), timeout | |
) | |
except AttributeError as exc: # pragma: nocover | |
if "resume_reading" in str(exc): | |
# Python's asyncio has a bug that can occur when a | |
# connection has been closed, while it is paused. | |
# See: https://github.com/encode/httpx/issues/1213 | |
# | |
# Returning an empty byte-string to indicate connection | |
# close will eventually raise an httpcore.RemoteProtocolError | |
# to the user when this goes through our HTTP parsing layer. | |
return b"" | |
raise | |
with map_exceptions(exc_map): | |
try: | |
return await asyncio.wait_for( | |
self._stream_reader.read(max_bytes), timeout | |
) | |
except AttributeError as exc: # pragma: nocover | |
if "resume_reading" in str(exc): | |
# Python's asyncio has a bug that can occur when a | |
# connection has been closed, while it is paused. | |
# See: https://github.com/encode/httpx/issues/1213 | |
# | |
# Returning an empty byte-string to indicate connection | |
# close will eventually raise an httpcore.RemoteProtocolError | |
# to the user when this goes through our HTTP parsing layer. | |
return b"" | |
raise |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
async with self._write_lock: | |
with map_exceptions(exc_map): | |
self._stream_writer.write(data) | |
return await asyncio.wait_for(self._stream_writer.drain(), timeout) | |
with map_exceptions(exc_map): | |
self._stream_writer.write(data) | |
return await asyncio.wait_for(self._stream_writer.drain(), timeout) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Uh oh!
There was an error while loading. Please reload this page.