【发布时间】:2018-08-20 12:56:09
【问题描述】:
我有一个 Flask 应用程序,在命令行上运行时可以正常工作,但是当它通过 uWSGI 运行时,它不能正确响应请求或工作线程不能正常工作。我重写了一个简单的概念验证/故障程序来演示这个问题:
from datetime import datetime
from threading import Event, Thread
from flask import Flask
class JobManager:
def __init__(self):
self.running = False
self.event = Event()
def start(self):
self.running = True
while self.running:
print("Processing Job at", datetime.now().strftime('%c'))
self.event.clear()
self.event.wait(5)
if self.event.is_set():
print("Interrupted by request!")
def stop(self):
self.running = False
self.event.set()
app = Flask(__name__)
jobs = JobManager()
t = Thread(target=jobs.start)
t.start()
@app.route('/')
def hello_world():
global jobs
jobs.event.set()
return "I'm alive at " + datetime.now().strftime('%c')
if __name__ == '__main__':
app.run()
我希望调用 / 路由会打印“被请求中断!”在控制台上,但它只是挂起,即使作业应该在单独的线程中运行。
我的uWSGI配置是:
[uwsgi]
module = app:app
master = true
processes = 5
threads = 2
socket = 0.0.0.0:5000
protocol = http
reload-mercy = 5
worker-reload-mercy = 5
die-on-term = true
enable-threads = true
thunder-lock = true
logto = /home/user/dev/flask-thread/uwsgi_log.log
logto2 = /home/user/dev/flask-thread/uwsgi2_log.log
env = PATH=/home/user/dev/flask-thread/env/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
然后我在 venv 中运行 uWSGI:uwsgi --ini uwsgi-test.ini
如果我使用python app.py 并使用内置的烧瓶开发服务器,它将起作用。
我唯一的猜测是它与 GIL 与 uWSGI 交互有关,但这是一个疯狂的猜测,我不知道如何阻止它。
【问题讨论】:
标签: python-3.x flask uwsgi python-multithreading gil