1

I am trying to update two values of a queue at the same time, but my code ends up in an infinite loop. I'm running it on Django View, the code below runs well but when I try it on Django it just keeps waiting. What could be happening?

import multiprocessing
from multiprocessing import Process

ret = {'algo' : 'hola', 'beto' : 'oka'}

def algo(queue):
    ret = queue.get()
    ret['algo'] = False #This is actually an API call value
    queue.put(ret)

def beta(queue):
    ret = queue.get()
    ret['beto'] = True #This is actually an API call value
    queue.put(ret)


queue = multiprocessing.Queue()
queue.put(ret)
p1 = Process(target=algo, args=(queue,))
p2 = Process(target=beta, args=(queue,))
p1.start()
p2.start()
p1.join()
p2.join()
q = queue.get()

Code in Django Views.py

ret = {'algo' : 'hola', 'beto' : 'oka'}

def algo(queue):
    ret = queue.get()
    ret['algo'] = False #This is actually an API call value
    queue.put(ret)

def beta(queue):
    ret = queue.get()
    ret['beto'] = True #This is actually an API call value
    queue.put(ret)

def audio(request):
    if request.method == 'POST':
        AUDIO_FILE = path.join(os.getcwd(), "audio.wav")
        # use the audio file as the audio source
        r = sr.Recognizer()
        with sr.AudioFile(AUDIO_FILE) as source:
            audio = r.record(source)

        queue = multiprocessing.Queue()
        queue.put(ret)
        p1 = Process(target=algo, args=(queue,))
        p2 = Process(target=beta, args=(queue,))
        p1.start()
        p2.start()
        p1.join()
        p2.join()
        q = queue.get()
        print(q)
Juanvulcano
  • 1,354
  • 3
  • 26
  • 44
  • Might be hard to say without knowing more about the "API call" in the subprocesses, and also how you're running Django. At what point is it hanging? Put in some logging if you're not sure. – Iguananaut May 20 '17 at 18:25
  • 1
    @Iguananaut Actually, the API call has nothing to do with it. I tried replacing it with other values and still is stuck. It get's stucks when trying to do the first join – Juanvulcano May 20 '17 at 18:43
  • I see now. This is basically this question: http://stackoverflow.com/questions/31665328/python-3-multiprocessing-queue-deadlock-when-calling-join-before-the-queue-is-em the fact that it happened to work outside of Django is probably just luck. These kinds of synchronization issues are often very sensitive to what else is going on around them. – Iguananaut May 20 '17 at 19:04

0 Answers0