sys.excepthook如果我尝试执行此代码,为什么不调用该函数?
import sys;
def MyExcepthook(ex_cls, ex, tb):
print("Oops! There's an Error.\n");
a=open("./ERR.txt","w"); #Fixed as suggested by unutbu BUT the problem is the same!
a.write("Oops! There's an Error.\n");
a.close();
sys.excepthook = MyExcepthook;
def main():
print(1/0);
if (__name__=="__main__"):
main();
Run Code Online (Sandbox Code Playgroud)
输出:
Traceback (most recent call last):
File "C:\Users\Path\to\my\python\file.py", line 13, in <module>
main();
File "C:\Users\Path\to\my\python\file.py", line 10, in main
print(1/0);
ZeroDivisionError: division by zero
Run Code Online (Sandbox Code Playgroud)
预期输出(按print):
Oops! There's an Error.
Run Code Online (Sandbox Code Playgroud)
并Err.txt应通过创建一个新文件(open)
该print函数不显示文本并且未创建文件,因为sys.excepthook未调用该函数-为什么?
-> …
问题出在哪儿?
void MyClass::task(void *pvParameter){
while(1){
this->update();
}
}
void MyClass::startTask(){
xTaskCreate(this->task, "Task", 2048, NULL, 5, NULL);
}
Run Code Online (Sandbox Code Playgroud)
但是,我明白了:
错误:非静态成员函数的无效使用
我找不到任何有用的文档来检查错误在哪里,
但我认为应该是这样的:(C++11's std::thread) 例如:
xTaskCreate(&MyClass::task, "Task", 2048, (void*)this, 5, NULL);
Run Code Online (Sandbox Code Playgroud)
对我有用的解决方案:
void MyClass::task(){
while(1){
this->update();
}
}
static void MyClass::startTaskImpl(void* _this){
static_cast<MyClass*>(_this)->task();
}
void MyClass::startTask(){
xTaskCreate(this->startTaskImpl, "Task", 2048, this, 5, NULL);
}
Run Code Online (Sandbox Code Playgroud) 当我的 Python3.6 Sanic Web 服务器与客户端应用程序失去连接时(例如:用户关闭 Web 浏览器或网络失败等...)
从 sanic 进口 Sanic
导入 sanic.response 作为响应
app = Sanic()
@app.route('/')
异步定义索引(请求):
return await response.file('index.html')
@app.websocket('/wsgate')
异步定义提要(请求,ws):
为真:
数据 = 等待 ws.recv()
打印('收到:' + 数据)
res = doSomethingWithRecvdData(data)
等待 ws.send(res)
如果 __name__ == '__main__':
app.run(host="0.0.0.0", port=8000, debug=True)