Skip to content Skip to sidebar Skip to footer

Python Multiprocessing Queue Is Empty Although It Is Filled In A Different Thread

I have now tried to resolve this issue for multiple hours but no matter what I do, I never get the thing to work. My project tracks live data and provides an endpoint for other ser

Solution 1:

I took the liberty of stripping out the CV2 code as I don't have a camera, and replace the queue filler with a pair of random numbers every .5 seconds, and PEP8-ing the code a bit, and this way it works:

import random
import time

from flask import Flask
import threading
from multiprocessing import Queue
from Queue import Empty as QueueEmpty

app = Flask(__name__)

classImageParser(object):
    def__init__(self, queue):
        self.queue = queue
        self.source = random.random
        self.pause = 0.5defrun(self):
        whileTrue:
            value = (self.source(), self.source())
            self.queue.put(value)
            time.sleep(self.pause)

queue = Queue()
image_parser = ImageParser(queue)
image_thread = threading.Thread(target=image_parser.run)

@app.route('/', methods=['GET'])defdo_main():
    try:
        value = queue.get_nowait()
    except QueueEmpty:
        value = Noneprint(value)
    returnstr(value)

if __name__ == '__main__':
    image_thread.start()
    app.run(debug=True, host='127.0.0.1')

Under http://127.0.0.1:5000/ I now get pairs of random numbers, and the occasional None when I reload too fast.

I therefore conclude that the problem probably lies with the image processing part. Specifically I noticed that only contours with an enclosing radius > 10 get put into the queue. Maybe that path of code just never gets executed. Are you quite sure that any values get put into the queue at all? Maybe a print x, y, radius before the if radius > 10 will shed some light. (And why put center instead of x and y?)

Post a Comment for "Python Multiprocessing Queue Is Empty Although It Is Filled In A Different Thread"