在python 2.7中并行运行函数,以在其他函数

时间:2017-02-08 19:59:04

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

我是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模块。有人可以告诉我如何克服这个问题。

1 个答案:

答案 0 :(得分: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时阻塞,但其他进程仍将继续运行。