Python 在django+;uWSGI

Python 在django+;uWSGI,python,django,multithreading,uwsgi,Python,Django,Multithreading,Uwsgi,我正在努力使Python标准队列在uWSGI master中工作 示例代码: import queue, threading, time from django.apps import AppConfig my_queue = queue.Queue() class MyApp(AppConfig): def ready(self): thread1 = threading.Thread(target=process_queue, args=()) t

我正在努力使Python标准队列在uWSGI master中工作

示例代码:

import queue, threading, time
from django.apps import AppConfig

my_queue = queue.Queue()

class MyApp(AppConfig):
    def ready(self):
        thread1 = threading.Thread(target=process_queue, args=())
        thread1.daemon = True
        thread1.start()

        thread2 = threading.Thread(target=poll, args=())
        thread2.daemon = True
        thread2.start()

def poll():
    while True:
        time.sleep(1)
        logging.info("Polling works every time")


def process_queue():
    while True:
        logging.info("I get here")
        email = my_queue.get(block=True, timeout=None)
        logging.info("I NEVER GET here. **I would like to get here**")

def put():
    # I call this from the django views on requests
    my_queue.put("1")
两个线程都启动。螺纹2按预期定期打印。线程1永远在队列上等待,即使我从主线程添加到队列中(作为Django请求的一部分,在视图中)

我的uWSGI配置:

[uwsgi]
master         = true
module         = backend.wsgi
processes      = 1
thunder-lock   = true
enable-threads = true
socket         = /tmp/django.sock
chmod-socket   = 666
vacuum         = true
请注意,如果我使用
master=false运行uWSGI,那么一切都会按预期工作

其他信息:

  • 我是作为附庸管理这件事的。皇帝不是以主人的身份参选
  • 我不太理解django+uWSGI中的前/后分叉。这可能是个问题
  • 如果不是以大师的身份运行,这是可行的,但我负担不起这样做
版本:

  • Python:3.6.3
  • Django:2.0.5
  • uWSGI:2.0.17

我也打开了这个问题

后台任务不应该在WSGI应用程序中运行。将其作为管理命令来实施。或者使用像芹菜这样的系统。谢谢你的回答@KlausD。然而,这并不能解决根本问题。我还没有机会改变架构,这是一个更复杂的应用程序的简化示例。WSGI应用程序的设计不是为了在其中运行额外的线程,因此您的架构是您观察的原因。症状可能因WSGI服务器设置而异。