运行Flas时为什么gUnicorn spaws 2进程

2024-05-13 14:54:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在运行一个Flask应用程序,它基本上是从Twitter上提取tweets。虽然使用嵌入式Flask服务器运行应用程序没有任何问题,但在gUnicorn中运行时,我会收到重复的tweet,主要是因为我有2个线程从Twitter接收回调。在

例如,如果我使用

Python应用程序副本在

当收到tweet时,我得到了这个预期的输出,请注意我在logger输出中附加了线程信息(第一个参数):

140721974449920 2015-03-12 17:59:13,030 INFO: Got message from streaming Twitter API! [in /home/mosquito/git/opencoast_streamer/app.py:83]
140721974449920 2015-03-12 17:59:14,646 INFO: Got message from streaming Twitter API! [in /home/mosquito/git/opencoast_streamer/app.py:83]
140721974449920 2015-03-12 17:59:49,031 INFO: Got message from streaming Twitter API! [in /home/mosquito/git/opencoast_streamer/app.py:83]

正如您所看到的,时间戳看起来也是有效的,检查我存储这个的mongo集合,我发现文档是可以的。那么,如果我使用gunicorn启动应用程序:

^{pr2}$

然后检查日志,我可以看到两个不同的线程正在获取数据:

139883969844992 2015-03-12 17:52:05,104 INFO: Got message from streaming Twitter API! [in /home/mosquito/git/opencoast_streamer/app.py:83]
139883961452288 2015-03-12 17:52:05,106 INFO: Got message from streaming Twitter API! [in /home/mosquito/git/opencoast_streamer/app.py:83]
139883969844992 2015-03-12 17:53:36,480 INFO: Got message from streaming Twitter API! [in /home/mosquito/git/opencoast_streamer/app.py:83]
139883961452288 2015-03-12 17:53:36,481 INFO: Got message from streaming Twitter API! [in /home/mosquito/git/opencoast_streamer/app.py:83]

正如你所看到的,发生了一些奇怪的事情……然后我去看并检查了古尼康:

ps aux | grep gunicorn

mosquito 25035  3.1  0.3  54612 12516 pts/1    S    15:31   0:01 /home/mosquito/www/env/bin/python /home/mosquito/www/env/bin/gunicorn app:app -b localhost:8000
mosquito 25606  0.0  0.4  66904 17016 pts/1    R    15:32   0:00 /home/mosquito/www/env/bin/python /home/mosquito/www/env/bin/gunicorn app:app -b localhost:8000
mosquito 25610  0.0  0.0  13220   956 pts/3    S+   15:32   0:00 grep --color=auto gunicorn

因此,我开始认为这与gUnicorn有关…你知道为什么gUnicorn为我的Flask应用程序Spawin2进程吗?在

谢谢!在


Tags: infrompygitinfoapiappmessage
3条回答

我相信这不是古尼科恩的错,而是韦克泽格的故意行为。Werkzeug有一个“重新加载程序”进程,用于监视文件更改(如果检测到.py文件中有更改,则重新加载)。在

有关重新加载程序go here的详细信息。在

为了让您度过难关,我相信在您的呼叫app.run:app.run(use_reloader=False)中添加{}就可以了。在

您还可以看到这个SO answer以获取更多信息。在

gunicorn  workers=1 app:app -b localhost:8000  debug

Source

即使在设置 workers=1时,Gunicorn始终至少生成2个进程。其中一个进程是主进程,它派生其他工作进程来处理请求。在

相关问题 更多 >