python - 在 python 2.7 中并行运行函数以在其他函数末尾使用函数的输出

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

我是Python的新手,从未使用过它的并行处理模块,例如线程多进程。我正在研究一个实时代码问题,其中一个函数的输出用作一个函数的输入。有一个大功能需要近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。我已经阅读了关于这个问题的多篇文章使用线程subprocessmultiprocessing python模块,但我无法得到这个问题的具体解决方案。我从其他帖子中得到的信息似乎是我需要使用multiprocessing模块。有人可以告诉我应该如何克服这个问题吗?

最佳答案

你可以这样做:

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 时主进程会阻塞,但其他进程仍会运行。

关于python - 在 python 2.7 中并行运行函数以在其他函数末尾使用函数的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42122501/

相关文章:

python中缀前向管道

python - Python 安全编码指南

linux - 我可以在 bash 中为多个并行后台进程使用相同的变量名吗?

c - 查找数组中最大数的多线程算法的性能

java - Kotlin 进程等待所有线程完成?

python - 类型错误 : Can't convert 'int' object to str implicitly

python - 行拆分后添加字典键和值?

python - 我正在使用 Python 中的基本脚本,但收到以下错误。这是什么意思?

mongodb - 在 pyspark RDD 上应用映射函数

python - 如何根据 pandas python 中的特定列合并两个数据框?