Here is the minimal code needed to reproduce the problem.
I call an API with a callback function that prints what comes out of the API call.
If I run this code in Jupyter, I get the output. If I run it with python file.py
I don't get any output. I already checked the API's code, but that does nothing weird. Setting DEBUGGING
to True
isn't helpful either.
Note that there is once dependency; the Bitvavo API. Install it with pip install python-bitvavo-api
# %%
from python_bitvavo_api.bitvavo import Bitvavo
# %%
def generic_callback(response):
print(f"log function=get_markets, {response=}")
bitvavo = Bitvavo({"DEBUGGING": False})
websocket = bitvavo.newWebsocket()
# %%
websocket.markets(options={"market": "BTC-EUR"}, callback=generic_callback)
Here is the expected output I get from Jupyter:
log function:get_markets, response={'market': 'BTC-EUR', 'status': 'trading', 'base': 'BTC', 'quote': 'EUR', 'pricePrecision': 5, 'minOrderInBaseAsset': '0.0001', 'minOrderInQuoteAsset': '5', 'orderTypes': ['market', 'limit', 'stopLoss', 'stopLossLimit', 'takeProfit', 'takeProfitLimit']}
CodePudding user response:
Because you're using a websocket, the callback is executed by a different thread, which means that if you don't wait, the main thread (which is to receive the print
's output) will already be killed.
Add a sleep(1)
(that's seconds, not ms) at the end and the output will show.
PS: The reason Jupyter does show the output, is because Jupyter keeps an interactive window open the entire time, even far after you've run the code :)
CodePudding user response:
import time
from python_bitvavo_api.bitvavo import Bitvavo
# %%
def generic_callback(response):
print(f"log function=get_markets, {response=}")
bitvavo = Bitvavo({"DEBUGGING": False})
websocket = bitvavo.newWebsocket()
# Wait N.1 required to receive output, otherwise the main thread is killed
time.sleep(1)
# %%
websocket.markets(options={"market": "BTC-EUR"}, callback=generic_callback)
# Wait N.2 required to receive output, otherwise the main thread is killed
time.sleep(1)