在Django+uWSGI中使用队列进行多线程处理

2024-04-25 23:27:03 发布

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

我正在努力使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配置:

^{pr2}$

请注意,如果我使用master = false运行uWSGI,则一切都按预期工作

附加信息:

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

版本:

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

我也打开了这一期https://github.com/unbit/uwsgi/issues/1860


Tags: djangoinfotruegetheretime队列queue

热门问题