-
-
Notifications
You must be signed in to change notification settings - Fork 522
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add repro.py script for debugging purposes
- Loading branch information
1 parent
0215b10
commit 179740c
Showing
1 changed file
with
84 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import asyncio | ||
import logging | ||
import json | ||
from websockets.asyncio.client import connect | ||
|
||
# import debugpy | ||
|
||
# # Allow VS Code to attach | ||
# debugpy.listen(("0.0.0.0", 5678)) # Use the port you've specified | ||
# print("Waiting for debugger to attach...") | ||
# debugpy.wait_for_client() | ||
|
||
logging.getLogger(__name__) | ||
logging.basicConfig( | ||
format="%(asctime)s %(module)s:%(lineno)d %(levelname)8s | %(message)s", | ||
datefmt="%Y/%m/%d %H:%M:%S", | ||
level=logging.DEBUG, | ||
) | ||
|
||
class MyClient: | ||
|
||
def __init__(self): | ||
self.keep_alive = True | ||
|
||
async def run(self): | ||
|
||
async with connect( | ||
f"wss://ws.kraken.com/v2", | ||
ping_interval=30, | ||
# max_queue=None, # having this enabled doesn't cause problems | ||
) as socket: | ||
await socket.send( | ||
json.dumps( | ||
{ | ||
"method": "subscribe", | ||
"params": { | ||
"channel": "book", | ||
"symbol": [ | ||
"BTC/USD", | ||
"DOT/USD", | ||
"ETH/USD", | ||
"MATIC/USD", | ||
"BTC/EUR", | ||
"DOT/EUR", | ||
"ETH/EUR", | ||
"XLM/USD", | ||
"XLM/EUR", | ||
], | ||
"depth": 100 | ||
}, | ||
} | ||
) | ||
) | ||
|
||
while self.keep_alive: | ||
try: | ||
_message = await asyncio.wait_for(socket.recv(), timeout=10) | ||
except TimeoutError: | ||
pass | ||
except asyncio.CancelledError: | ||
self.keep_alive = False | ||
else: | ||
try: | ||
message = json.loads(_message) | ||
except ValueError: | ||
pass | ||
|
||
async def __aenter__(self): | ||
self.task: asyncio.Task = asyncio.create_task(self.run()) | ||
return self | ||
|
||
async def __aexit__(self, *args, **kwargs): | ||
self.keep_alive = False | ||
if hasattr(self, "task") and not self.task.done(): | ||
await self.task | ||
|
||
|
||
async def main(): | ||
async with MyClient(): | ||
await asyncio.sleep(3) | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |