Skip to main content
 首页 » 编程设计

python之wsgi之处理客户端超时

2024年11月01日10虾米姐

我正在做一些基本的 wsgi 东西:

def application(environ, start_response): 
    start_response("200", []) 
    result = some_long_func() 
    return [result] 

客户端在 some_long_func 完成之前断开连接的情况时有发生,我在日志中看到以下内容:

SIGPIPE: writing to a closed pipe/socket/fd (probably the client disconnected)... 

有没有办法在应用程序级别处理断开连接的客户端,即在 application() 返回之前?

我的服务器是 uwsgi(独立)。没有框架,只有纯 Python。

请您参考如下方法:

If the iterable returned by the application has a close() method, the server or gateway must call that method upon completion of the current request, whether the request was completed normally, or terminated early due to an error.

因此,如果在输出迭代器完成之前调用了 close(),您就知道客户端已断开连接。