Django - 多进程队列不更新

时间:2017-05-20 13:58:47

标签: python django multiprocessing

我正在尝试同时更新队列的两个值,但我的代码最终处于无限循环中。我在Django View上运行它,下面的代码运行良好但是当我在Django上尝试它时它只是在等待。可能会发生什么?

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

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)

0 个答案:

没有答案
相关问题