forked from jupyter-server/jupyter_ydoc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Backport PR jupyter-server#288 on branch 2.x (Fix tests)
- Loading branch information
1 parent
8f8a1ca
commit af6d5c0
Showing
5 changed files
with
79 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Copyright (c) Jupyter Development Team. | ||
# Distributed under the terms of the Modified BSD License. | ||
|
||
from anyio import Lock, connect_tcp | ||
|
||
|
||
class Websocket: | ||
def __init__(self, websocket, path: str): | ||
self._websocket = websocket | ||
self._path = path | ||
self._send_lock = Lock() | ||
|
||
@property | ||
def path(self) -> str: | ||
return self._path | ||
|
||
def __aiter__(self): | ||
return self | ||
|
||
async def __anext__(self) -> bytes: | ||
try: | ||
message = await self.recv() | ||
except Exception: | ||
raise StopAsyncIteration() | ||
return message | ||
|
||
async def send(self, message: bytes): | ||
async with self._send_lock: | ||
await self._websocket.send_bytes(message) | ||
|
||
async def recv(self) -> bytes: | ||
b = await self._websocket.receive_bytes() | ||
return bytes(b) | ||
|
||
|
||
async def ensure_server_running(host: str, port: int) -> None: | ||
while True: | ||
try: | ||
await connect_tcp(host, port) | ||
except OSError: | ||
pass | ||
else: | ||
break |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters