這是重現問題所需的最少代碼。
我呼叫帶有回呼函式的 API,該函式列印 API 呼叫的結果。
如果我在 Jupyter 中運行此代碼,則會得到輸出。如果我運行它,python file.py我不會得到任何輸出。我已經檢查了 API 的代碼,但這并沒有什么奇怪的。設定DEBUGGING為True也無濟于事。
注意這里有一次依賴;Bitvavo API。安裝它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)
這是我從 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']}
uj5u.com熱心網友回復:
因為您使用的是 websocket,回呼是由不同的執行緒執行的,這意味著如果您不等待,主執行緒(即接收print's 輸出)將已經被殺死。
sleep(1)在末尾添加一個(即秒,而不是毫秒),輸出將顯示。
PS:Jupyter確實顯示輸出的原因是因為 Jupyter 始終保持互動式視窗打開,即使在您運行代碼之后很長時間:)
uj5u.com熱心網友回復:
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/390774.html
標籤:Python 蟒蛇-3.x jupyter-笔记本 jupyter
上一篇:【資料結構與演算法】堆疊與佇列
下一篇:加速串列迭代瓶頸
