小编Ner*_*ine的帖子

sys.excepthook为什么不起作用?

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未调用该函数-为什么?

-> …

python sys

6
推荐指数
1
解决办法
2668
查看次数

C++ freeRTOS 任务,非静态成员函数的无效使用

问题出在哪儿?

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)

c++ multithreading task freertos

6
推荐指数
1
解决办法
2491
查看次数

我可以使用 Python3.6 Sanic 检测 websockets 中的“连接丢失”吗?

当我的 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)

websocket python-3.6 sanic

1
推荐指数
1
解决办法
2782
查看次数

标签 统计

c++ ×1

freertos ×1

multithreading ×1

python ×1

python-3.6 ×1

sanic ×1

sys ×1

task ×1

websocket ×1