gpt4 book ai didi

python - Python不抛出错误或异常,websocket on_message

转载 作者:行者123 更新时间:2023-12-03 07:51:52 27 4
gpt4 key购买 nike

当我将太多参数传递给f()时,为什么python不抛出错误或异常?显然,由于该错误,未执行f()。我正在使用VS-Code和python 3.8。 (如果要复制,则必须安装websocket_client软件包)

import websocket
import json

connection = "wss://ws-feed.pro.coinbase.com"
subscription = json.dumps({
"type": "subscribe",
"channels": [
{
"name": "level2",
"product_ids": ["BTC-USD"]
}
]
})

def f(msg):
print(msg)

def ws_message(ws, message):
f("hi",True) #<--should throw a too many arguments error/exception


def ws_open(ws):
ws.send(subscription)

def ws_thread(*args):
ws = websocket.WebSocketApp(
connection, on_message=ws_message, on_open=ws_open)
ws.run_forever()

ws_thread()

最佳答案

这是由于实现了here的故障安全机制所致。如果您想跟踪异常,则应为记录器设置适当的级别。为此,请执行以下操作:

import websocket
websocket._logging._logger.level = -99 # This will enable all levels of logging

# Rest of your code goes here
这样,运行代码后,您将看到:
  File "blablabla/.local/lib/python3.7/site-packages/websocket/_app.py", line 346, in _callback
callback(self, *args)
File "blablabla/test/test.py", line 22, in ws_message
f("hi", True) #<--should throw a too many arguments error/exception
您的代码仍将运行,但是您将能够捕获异常

关于python - Python不抛出错误或异常,websocket on_message,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62918770/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com