Python多进程队列始终为空

2024-10-04 05:25:42 发布

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

无缘无故,即使我在队列中放了一些项目,它仍然显示我

Queue.empty() == True

工人们正在使用一种与烧瓶法不同的工艺。如果我试图使用一些Flask sub将项目添加到队列中,然后检查队列是否不是空的(使用另一个Flask sub),我可以看到队列已满。但是我仍然从workerJob子节点得到“queue is empty”

from multiprocessing import Queue, Process, Pool


imgQueue = Queue()

@app.route("/send", methods=["POST"])
def sendToQueue():
    # global imgQueue

    imgLocation = request.headers.get('fileLocation')
    nImage = request.data

    imgQueue.put({"location": imgLocation,
                  "image": nImage})


    return "ok"

def workerJob(q):


    while True:
        print(q.empty())
        if (not q.empty()):

            itemFromQueue = q.get()
            print(str(type(itemFromQueue)))
            print(itemFromQueue["location"])
            print(itemFromQueue["image"])


        else:
            print("Queue empty")


        time.sleep(3)

def startWorkers():
    global imgQueue

    while True:
        procs = [Process(target=workerJob, args=(imgQueue,)) for i in range(1)]
        for p in procs:
            p.start()

def flaskThread():
    app.run(host='0.0.0.0', threaded=True)

if __name__ == '__main__':

  # app.run(host='0.0.0.0', threaded=True)
  flaskProcess = Process(target=flaskThread, args=())
  flaskProcess.start()
  workerProcess = Process(target=startWorkers, args=())
  workerProcess.start()

Tags: 项目trueapptarget队列queuedefargs