Python 多处理程序未运行到最后

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

我是 python 多重处理的新手。

基本上我的问题场景是我想在一组表(比如 2 个表)上并行运行我的 python 脚本。

这里我的Python脚本并行地从每个表中读取数据,然后将每个表中的数据写入另一个表中。

我编写了以下代码片段来创建多进程 python 脚本。但是,当我运行脚本时,它没有完成,也没有抛出任何错误消息。

count = multiprocessing.cpu_count()
pool = multiprocessing.Pool(processes=count)
args = [ ('yelp','localhost:9160','cassa1','flight88'), ('yelp','localhost:9160','cassa1','flight96') ]
for a in args:
    print a
    pool.apply_async(user_input,a)

感谢对此的帮助,因为我很困惑并且被困在这里。

最佳答案

您的脚本在子进程完成其任务之前退出。在末尾添加:

pool.close() # no more tasks
pool.join()  # wait for the remaining tasks to complete

此外,您可以使用 pool.imap*() 方法来代替:

from multiprocessing import Pool

def safe_user_input(args):
    try:
         return user_input(*args), None
    except Exception as e:
         return None, str(e)

if __name__=="__main__":       
   tables = [
        ('yelp','localhost:9160','cassa1','flight88'),
        ('yelp','localhost:9160','cassa1','flight96')
   ]

   pool = Pool() # use all available CPUs
   for result, error in pool.imap_unordered(safe_user_input, tables):
       if error is None: # no error
          print(result)

关于Python 多处理程序未运行到最后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21749091/

相关文章:

c - MPI 如何决定我们创建多少个进程

python - tflearn/tensorflow 不学习 xor

python - 如何在Python中逐像素分析图像

Python 绘图 - 循环遍历特征和绘图的唯一值

python - 覆盆子PI B +上的python 2.7 cvtColor错误215

python pdfkit : increase font size

linux - 如何在 sh 文件中并行运行 shell 脚本命令?

python - 改变方程的 ast.NodeTransformer 示例

python - for 循环里面的 if 条件

linux - 如何提高Linux集群上计算节点的并行处理速度?