小编n4n*_*355的帖子

使用uWSGI进行Python3线程处理

我浪费了很多时间,但找不到解决方案.
如果我在使用uwsgi部署的应用程序中使用线程,则它们不同步.

这里是一个简单的代码示例(wsgi.py):

from time import sleep
import threading

i = 0
def daemon():
  global i
  while True:
    i += 1
    print(i)
    sleep(3)
th = threading.Thread(target=daemon, args=())
th.start()

def application(environ, start_response):
  start_response('200 OK', [('Content-Type','text/html')])
  return [str(i).encode()]
Run Code Online (Sandbox Code Playgroud)

当我运行这个应用程序i增加日志,但我总是得到1来自浏览器的make请求.(或者0如果我sleep(3)i第一次增量之前移动)
我尝试了uwsgi.thread装饰,但得到了相同的结果.

uwsgi配置:

[uwsgi]
socket = 127.0.0.1:3034
plugins-dir = /srv/uwsgi
plugin = python34
uid = py3utils
gid = py3utils
chdir = /srv/python/3/py3utils/tht/app/
wsgi-file = wsgi.py
enable-threads = true
daemonize = %(chdir)/../uwsgi.log
master = true …
Run Code Online (Sandbox Code Playgroud)

python multithreading python-multithreading uwsgi python-3.x

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