python多进程队列始终为空

时间:2018-12-29 21:54:18

标签: python threadpool

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

Queue.empty() == True

工人正在使用与Flask不同的流程运行。如果我尝试使用一些Flask子项将项目添加到队列中,然后检查队列是否不为空(使用另一个Flask子项),则可以看到队列已满。但是我仍然从workerJob子项中获得“队列为空”。

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()

0 个答案:

没有答案