Skip to content

Commit

Permalink
Move asyncio compatibility to a new package.
Browse files Browse the repository at this point in the history
  • Loading branch information
aaugustin committed Apr 7, 2023
1 parent 8c6f726 commit 570da0a
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 11 deletions.
Empty file.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/websockets/legacy/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
cast,
)

from ..asyncio.compatibility import asyncio_timeout
from ..datastructures import Headers, HeadersLike
from ..exceptions import (
InvalidHandshake,
Expand All @@ -44,7 +45,6 @@
from ..http import USER_AGENT
from ..typing import ExtensionHeader, LoggerLike, Origin, Subprotocol
from ..uri import WebSocketURI, parse_uri
from .compatibility import asyncio_timeout
from .handshake import build_request, check_response
from .http import read_response
from .protocol import WebSocketCommonProtocol
Expand Down
2 changes: 1 addition & 1 deletion src/websockets/legacy/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
cast,
)

from ..asyncio.compatibility import asyncio_timeout
from ..datastructures import Headers
from ..exceptions import (
ConnectionClosed,
Expand All @@ -53,7 +54,6 @@
)
from ..protocol import State
from ..typing import Data, LoggerLike, Subprotocol
from .compatibility import asyncio_timeout
from .framing import Frame


Expand Down
2 changes: 1 addition & 1 deletion src/websockets/legacy/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
cast,
)

from ..asyncio.compatibility import asyncio_timeout
from ..datastructures import Headers, HeadersLike, MultipleValuesError
from ..exceptions import (
AbortHandshake,
Expand All @@ -46,7 +47,6 @@
from ..http import USER_AGENT
from ..protocol import State
from ..typing import ExtensionHeader, LoggerLike, Origin, Subprotocol
from .compatibility import asyncio_timeout
from .handshake import build_response, check_request
from .http import read_request
from .protocol import WebSocketCommonProtocol
Expand Down
15 changes: 8 additions & 7 deletions src/websockets/sync/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ def get(self, timeout: Optional[float] = None) -> Data:
Raises:
EOFError: If the stream of frames has ended.
RuntimeError: If two threads run :meth:`get` or :meth:``get_iter`
RuntimeError: If two threads run :meth:`get` or :meth:`get_iter`
concurrently.
TimeoutError: If a timeout is provided and elapses before a
complete message is received.
"""
with self.mutex:
Expand Down Expand Up @@ -131,7 +133,7 @@ def get_iter(self) -> Iterator[Data]:
Raises:
EOFError: If the stream of frames has ended.
RuntimeError: If two threads run :meth:`get` or :meth:``get_iter`
RuntimeError: If two threads run :meth:`get` or :meth:`get_iter`
concurrently.
"""
Expand Down Expand Up @@ -159,11 +161,9 @@ def get_iter(self) -> Iterator[Data]:
self.get_in_progress = True

# Locking with get_in_progress ensures only one thread can get here.
yield from chunks
while True:
chunk = self.chunks_queue.get()
if chunk is None:
break
for chunk in chunks:
yield chunk
while (chunk := self.chunks_queue.get()) is not None:
yield chunk

with self.mutex:
Expand Down Expand Up @@ -242,6 +242,7 @@ def put(self, frame: Frame) -> None:
self.put_in_progress = True

# Release the lock to allow get() to run and eventually set the event.
# Locking with get_in_progress ensures only one coroutine can get here.
self.message_fetched.wait()

with self.mutex:
Expand Down
2 changes: 1 addition & 1 deletion tests/legacy/test_client_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import urllib.request
import warnings

from websockets.asyncio.compatibility import asyncio_timeout
from websockets.datastructures import Headers
from websockets.exceptions import (
ConnectionClosed,
Expand All @@ -29,7 +30,6 @@
)
from websockets.http import USER_AGENT
from websockets.legacy.client import *
from websockets.legacy.compatibility import asyncio_timeout
from websockets.legacy.handshake import build_response
from websockets.legacy.http import read_response
from websockets.legacy.server import *
Expand Down

0 comments on commit 570da0a

Please sign in to comment.