运行 Flask with Gunicorn 得到 "Error handling request [count must be a positive integer (got 0)]"

run Flask with Gunicorn got "Error handling request [count must be a positive integer (got 0)]"

版本:Python3.8.2 Flask 2.0.1 Werkzeug 2.0.1 Gunicorn 20.1.0

运行 flask in python3.7 or 3.8, gunicorn mode is 'sync' or 'gevent/eventlet', 我在终端得到同样的错误:“count must be一个正整数(得到0)”,但是浏览器中的网站似乎没有异常,chrome控制台也没有报错。

Gunicorn 配置:

# gunicorn.conf.py
workers = 5
worker_class = "sync"  # or "gevent"
bind = "0.0.0.0:8080"

输出:

>>> gunicorn application:app -c ./gunicorn.conf.py

Starting gunicorn 20.1.0
Listening at: http://0.0.0.0:8080 (69063)
Using worker: sync   # or "gevent"
Booting worker with pid: 69065
Booting worker with pid: 69066
Booting worker with pid: 69067
Booting worker with pid: 69068
Booting worker with pid: 69069
[69065] [ERROR] Error handling request
Traceback (most recent call last):
  File "/Users/xxx/.virtualenvs/yyy/lib/python3.8/site-packages/gunicorn/workers/sync.py", line 182, in handle_request
    resp.write_file(respiter)
  File "/Users/xxx/.virtualenvs/yyy/lib/python3.8/site-packages/gunicorn/http/wsgi.py", line 385, in write_file
    if not self.sendfile(respiter):
  File "/Users/xxx/.virtualenvs/yyy/lib/python3.8/site-packages/gunicorn/http/wsgi.py", line 375, in sendfile
    self.sock.sendfile(respiter.filelike, count=nbytes)
  File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/socket.py", line 482, in sendfile
    return self._sendfile_use_sendfile(file, offset, count)
  File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/socket.py", line 346, in _sendfile_use_sendfile
    self._check_sendfile_params(file, offset, count)
  File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/socket.py", line 460, in _check_sendfile_params
    raise ValueError(
ValueError: count must be a positive integer (got 0)

我在模板中使用了 url_for 函数来引用一个空的 css 文件,这导致了异常。

在已接受答案的相关注释中,我从模板头部的空 JavaScript 文件引用中得到了错误。

<script src="static/js/main.js"></script>

一旦我删除了引用,错误就消失了。我只是想区分一下,无论文件类型如何,对空文件的引用都会导致错误,而且它不仅仅与 url_for 函数有关。