Closed mfiro closed 3 years ago
actually that should work, i tested your code and if I hit ctrl+c it stops ...
Which OS do you use?
Maybe hit ctrl+c a second or third time, does this help?
In Windows 10 it doens't stop. I tried to hit ctrl-c multiple time. Actually the problem is that KeyboardInterrupt exception is not caught at all (I don't know why). When I tried this with Raspbian (debian based os) ctrl-c stops the script but again not by catching exception.
I'm not even sure, if I need a KeyboardInterrupt exception to close the program with ctrl-c.
i can confirm, its also not caught on linux.
I'm not even sure, if I need a KeyboardInterrupt exception to close the program with ctrl-c.
Actually not, its going to get closed by ctrl-c itself, but with catching the exception you can decide to not stop or do some logging or any action before you close.
That's true. Actually the reason I wrote exception handling in the first place was because I couldn't stop the script with ctrl+c. So I tried exception handling with sys.exit(0). But it didn't work either.
Hm. Maybe you could wait for a console input that is equal to "exit" in your script and if met, you exit the script...
This solves the problem:
if __name__ == '__main__':
binance_websocket_api_manager = BinanceWebSocketApiManager(exchange="binance.com")
# start a worker process to process to move the received stream_data from the stream_buffer to a print function
worker_thread = threading.Thread(target=print_stream_data_from_stream_buffer, args=(binance_websocket_api_manager,))
worker_thread.start()
kline_stream_id = binance_websocket_api_manager.create_stream(['kline', 'kline_1m'], ['btcusdt'])
try:
while True:
time.sleep(60)
except KeyboardInterrupt:
binance_websocket_api_manager.stop_stream(kline_stream_id)
binance_websocket_api_manager.stop_manager_with_all_streams()
The exception is also caught properly. The time.sleep() amount doesn't matter also in functionality as far as I've experienced.
Thank you very much for sharing your solution! I think i am going to put it into an example! best regards!
I uploaded an example: https://github.com/oliver-zehentleitner/unicorn-binance-websocket-api/blob/master/example_ctrl-c.py
@mfiro Thanks for sharing the snippet! I just saw that you are importing UnicornFy, thats not neccessary anymore, its a dependency of unicorn-binance-websocket-api.
just use output_default parameter for all streams as mentioned here: https://oliver-zehentleitner.github.io/unicorn-binance-websocket-api/unicorn_binance_websocket_api.html#unicorn_binance_websocket_api.unicorn_binance_websocket_api_manager.BinanceWebSocketApiManager
or in create_stream() the output parameter as mentioned here: https://oliver-zehentleitner.github.io/unicorn-binance-websocket-api/unicorn_binance_websocket_api.html#unicorn_binance_websocket_api.unicorn_binance_websocket_api_manager.BinanceWebSocketApiManager.create_stream
Thank you for adding it to examples and also for pointing that out:)
I'll then use the parameter instead of that.
Hi! Firstly thank you for this awesome project.
Sorry, if I ask, but I don't understand how to stop a running script in
while True
loop with ctrl-c and close everything properly. I tried to use the following code , however, the stream keeps printing and doesn't stop the process. What is the proper and clean way to do this?