【问题标题】:Run functions parallel in python 2.7 to use output of an function at the end of other functions在 python 2.7 中并行运行函数以在其他函数的末尾使用函数的输出
【发布时间】:2017-02-08 19:59:04
【问题描述】:

我是 python 的新手,从未使用过它的并行处理模块,如 threadingmultiprocess。我正在研究一个实时代码问题,其中一个函数的输出用作一个函数的输入。有一个大功能需要将近 3 秒才能完成。这就像一个程序,其中一个人将一些文件提交给接待处,并且在验证他的文件时,他被引导到其他地方进行不同的检查。如果在这些检查结束时文件验证结果可用,则程序将失败。

def parallel_running_function(*args):
     """It is the function which will take 3 seconds to complete"""
     output = "various documents matching and verification"
     return output

def check_1(*args):
    """ check one for the task"""

def check_2(*args):
    """ check two for the task"""

def check_3(*args):
    """ check three for the task"""

def check_4(*args):
    """ check 4 for the task"""


def main_function():

    output = parallel_running_function() # need to run this function 
                                        #parallel with other functions
    output_1 = check_1()
    output_2 = check_2()
    output_3 = check_3()
    output_4 = check_4()
    if output:
       "program is successful"
    else:
        "program is failed"

    I need the output of parallel running function is here along with the other executed functions. If I don't get the output of that function here then program will be failed or ll give some wrong result.

我正在使用 python 2.7。我已经阅读了多篇关于这个问题的帖子using threadingsubprocessmultiprocessing python 模块,但我无法得到这个问题的具体解决方案。我从其他帖子中得到的似乎是我需要使用multiprocessing 模块。有人可以给我一个想法,我应该如何克服这个问题。

【问题讨论】:

    标签: python python-2.7 parallel-processing python-multithreading


    【解决方案1】:

    你可以这样做:

    import multiprocessing
    
    pool = None
    
    def parallel_running_function(*args):
         """It is the function which will take 3 seconds to complete"""
         output = "various documents matching and verification"
         return output
    
    def check_1(*args):
        """ check one for the task"""
    
    def check_2(*args):
        """ check two for the task"""
    
    def check_3(*args):
        """ check three for the task"""
    
    def check_4(*args):
        """ check 4 for the task"""
    
    
    def main_function():
    
        res = pool.apply_async(parallel_running_function)
    
        res_1 = pool.apply_async(check_1)
        res_2 = pool.apply_async(check_2)
        res_3 = pool.apply_async(check_3)
        res_4 = pool.apply_async(check_4)
    
        output = res.get()
        output_1 = res_1.get()
        output_2 = res_2.get()
        output_3 = res_3.get()
        output_4 = res_4.get()
    
        if output:
           print "program is successful"
        else:
            print "program is failed"
    
    
    if __name__ == '__main__':
        pool = multiprocessing.Pool(processes=4)
        main_function()
    

    调用get时主进程会阻塞,但其他进程仍在运行。

    【讨论】:

    • 感谢@eveatles 的回答,我一直在寻找它。但是,如果 parallel_running_function 是类的成员或实例方法,为什么 response.get() 会失败。它在函数上运行良好,但在实例方法上产生了问题。
    • 这是因为多处理产生了新的进程而不是线程。因为对象具有无法在内存中跨进程共享的内部状态,所以您必须使用函数。如果你真的想使用方法,你可以使用线程来代替。
    • 非常感谢您的详细解答和回复
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多