以下python代码中的多重处理不起作用 [英] Multiprocessing in following python code does not work

查看:90
本文介绍了以下python代码中的多重处理不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过使用一些多处理来增加我的python程序的执行时间. 假设我有以下示例代码:

I'm trying to increase the execution time of my python program by using some multiprocessing. Suppose I have this sample code:

def foo(q,x,y):
     ....
     q.put(result)

def parallel_funtion(x):

    q1 = Queue(); q2 = Queue()

    p1 = Process(target=foo,
                 args=[q1,x,0])

    p2 = Process(target=foo,
                 args=[q2,x,1])

    p1.start(); p2.start()
    p1.join(); p2.join()

    z = max(q1.get(), q2.get())

    return z

def function(list)
    .....

    for i in list:
        parallel_function(i)


main():
    function(aList)

在功能"中循环的第一次迭代之后,该程序特别冻结在此行中:

After the first iteration in the cycle in "function" the program freezes specifically in this row:

z = max(q1.get(), q2.get())

为什么?

推荐答案

问题在细节上很短,但这对我有用...我修改了您对list的使用,因为这似乎破坏了python的list方法(尽管您仍然说过,代码仍会执行):

The question is short on specifics, but this works for me... I modified your use of list, since that appears to destroy python's list method (although as you say, the code still executes):

from multiprocessing import Process, Queue
import time

def foo1(queue, procid, arg1, arg2):
    # Measure execution time and return the total time in the queue
    print "%s got arg1=%s, arg2=%s " % (procid, arg1, arg2)
    start = time.time()
    ii = arg1
    while (ii > 0):
        ii = ii - 1
        time.sleep(0.01)
    # return the output of the call through the Queue
    queue.put((time.time() - start)*arg2)

def parallel_function(x):
    q1 = Queue()
    q2 = Queue()

    p1 = Process(target=foo1, args=[q1, 'Proc1', x, 1])
    p2 = Process(target=foo1, args=[q2, 'Proc2', x, 2])

    p1.start(); p2.start()
    p1.join(); p2.join()
    # Get return values from each Queue
    z = max(q1.get(), q2.get())
    return z

def function(_list):
    for ii in _list:
        print "FUNCTION RESULT input=%s, result=%s" % (ii, 
            parallel_function(ii))



function([100,120,130,140,150])

输出:

Proc1 got arg1=100, arg2=1 
Proc2 got arg1=100, arg2=2 
FUNCTION RESULT input=100, result=2.01133012772
Proc1 got arg1=120, arg2=1 
Proc2 got arg1=120, arg2=2 
FUNCTION RESULT input=120, result=2.4130563736
Proc1 got arg1=130, arg2=1 
Proc2 got arg1=130, arg2=2 
FUNCTION RESULT input=130, result=2.61448001862
Proc1 got arg1=140, arg2=1 
Proc2 got arg1=140, arg2=2 
FUNCTION RESULT input=140, result=2.81632232666
Proc1 got arg1=150, arg2=1 
Proc2 got arg1=150, arg2=2 
FUNCTION RESULT input=150, result=3.01693964005

这篇关于以下python代码中的多重处理不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆