Skip to content

Commit 8798828

Browse files
exception handling in user code (not ready)
1 parent 0b541b4 commit 8798828

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

README.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,18 @@ ubwa.api.cancel_order(orig_client_order_id=orig_client_order_id, symbol="BUSDUSD
177177
guide on
178178
[how to process requests via the Binance WebSocket API](https://medium.lucit.tech/create-and-cancel-orders-via-websocket-on-binance-7f828831404)!
179179

180+
## Stop `ubwa` after usage to avoid memory leaks
181+
When you instantiate UBWA with `with`, `ubwa.stop_manager()` is automatically executed upon exiting the `with`-block.
182+
```
183+
with BinanceWebSocketApiManager() as ubwa:
184+
ubwa.create_stream(channels="trade", markets="btcusdt", stream_label="TRADES")
185+
```
186+
187+
Without `with`, you must explicitly execute `ubwa.stop_manager()` yourself.
188+
```
189+
ubwa.stop_manager()
190+
```
191+
180192
## STREAM SIGNALS - know the state of your streams
181193
Usually you want to know when a stream is working and when it is not. This can be useful to know that your own system is
182194
currently "blind" and you may want to close open positions to be on the safe side, know that indicators will now provide
@@ -187,10 +199,19 @@ For this purpose, the UNICORN Binance WebSocket API provides so-called
187199
, which are used to tell your code in real time when a stream is connected, when it received its first data record, when
188200
it was disconnected and stopped, and when the stream cannot be restored.
189201

190-
## Stop `ubwa` after usage to avoid memory leaks
191-
192202
```
193-
ubwa.stop_manager()
203+
from unicorn_binance_websocket_api import BinanceWebSocketApiManager
204+
import time
205+
206+
def process_stream_signals(signal_type=None, stream_id=None, data_record=None, error_msg=None):
207+
print(f"Received stream_signal for stream '{ubwa.get_stream_label(stream_id=stream_id)}': "
208+
f"{signal_type} - {stream_id} - {data_record} - {error_msg}")
209+
210+
with BinanceWebSocketApiManager(process_stream_signals=process_stream_signals) as ubwa:
211+
ubwa.create_stream(channels="trade", markets="btcusdt", stream_label="TRADES")
212+
time.sleep(2)
213+
print(f"Waiting 5 seconds and then stop the stream ...")
214+
time.sleep(5)
194215
```
195216

196217
[Discover even more possibilities](https://unicorn-binance-websocket-api.docs.lucit.tech/unicorn_binance_websocket_api.html)

dev/test_stream_signals.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
#!/usr/bin/env python3
2-
# -*- coding: utf-8 -*-
3-
41
from unicorn_binance_websocket_api import BinanceWebSocketApiManager
52
import time
63

7-
8-
def receive_stream_signal(self, signal_type=None, stream_id=None, data_record=None, error_msg=None):
4+
def process_stream_signals(signal_type=None, stream_id=None, data_record=None, error_msg=None):
95
print(f"Received stream_signal for stream '{ubwa.get_stream_label(stream_id=stream_id)}': "
106
f"{signal_type} - {stream_id} - {data_record} - {error_msg}")
117

12-
13-
with BinanceWebSocketApiManager(process_stream_signals=receive_stream_signal) as ubwa:
14-
ubwa.create_stream(channels="trade", markets="btcusdt", stream_label="HEALTHY")
8+
with BinanceWebSocketApiManager(process_stream_signals=process_stream_signals) as ubwa:
9+
ubwa.create_stream(channels="trade", markets="btcusdt", stream_label="TRADES")
10+
time.sleep(2)
1511
print(f"Waiting 5 seconds and then stop the stream ...")
1612
time.sleep(5)
1713

unicorn_binance_websocket_api/manager.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -864,8 +864,7 @@ def _create_stream_thread(self,
864864
f"\033[1m\033[31m{type(error_msg).__name__} - {error_msg}\033[0m\r\n"
865865
f"{traceback.format_exc()}")
866866
print(f"\r\n{error_msg_wrapper}")
867-
error_msg_wrapper = (f"Exception within to UBWA`s provided `process_asyncio_queue`-coroutine of stream "
868-
f"'{stream_id}'{stream_label}: "
867+
error_msg_wrapper = (f"Exception within a coroutine of stream '{stream_id}'{stream_label}: "
869868
f"{type(error_msg).__name__} - {error_msg}\r\n"
870869
f"{traceback.format_exc()}")
871870
logger.critical(error_msg_wrapper)

0 commit comments

Comments
 (0)