简体   繁体   English

多处理 - 如何并行运行多个进程

[英]Multiprocessing - how to run multiple processes in parallel

Recently, I've been experimenting with the multiprocessing module.最近,我一直在试验 multiprocessing 模块。 I wrote this script to test it:我写了这个脚本来测试它:

import multiprocessing
from time import sleep
import datetime

def b(m):
   print(m)

def int_val(a):
   b(a)

def char_val(a):
   sleep(15)
   b(a)

list_val = [1,2,'c',6,10,1,'e',11,78,'a', 'b']

if __name__ == '__main__':
   p = multiprocessing.Pool(4)
   for index, val in enumerate(list_val):
      if isinstance(val, str):
         p.map(char_val, [val])
         print(datetime.datetime.now())
      else:
         p.map(int_val, [val])
         print(datetime.datetime.now())

The output looks like this :输出如下所示:

1
2020-03-29 01:45:37.099114
2
2020-03-29 01:45:37.099114
c
2020-03-29 01:45:52.114733
6
2020-03-29 01:45:52.114733
10
2020-03-29 01:45:52.114733
1
2020-03-29 01:45:52.114733
e
2020-03-29 01:46:07.115963
11
2020-03-29 01:46:07.115963
78
2020-03-29 01:46:07.115963
a
2020-03-29 01:46:22.117232
b
2020-03-29 01:46:37.118046

If we see the output, the program has not really achieved multi-processing I was looking for.如果我们看到输出,该程序并没有真正实现我正在寻找的多处理。

I was hoping that while the program waits for the 15 seconds for the element 'c', other elements will be processed and almost all the characters will be printed at the same time.我希望当程序等待元素 'c' 的 15 秒时,其他元素将被处理并且几乎所有字符都将同时打印。

It might be a silly question, but I am really lost!这可能是一个愚蠢的问题,但我真的迷路了!

Any help would be highly appreciated!任何帮助将不胜感激!

You're calling p.map iteratively for each element of the list, so essentially you're spawning a new process for each element.您正在为列表的每个元素迭代调用p.map ,因此本质上您正在为每个元素生成一个新进程。 Since p.map is a blocking call, it is actually waiting for execution of char_val('c') before moving ahead.由于p.map是一个阻塞调用,它实际上在继续之前等待执行char_val('c')

If you give the whole list_val to p.map() , you should get the expected execution sequence如果你把整个list_valp.map() ,你应该得到预期的执行顺序

import multiprocessing
from time import sleep
import datetime

def b(m):
   print(m)

def int_val(a):
   b(a)

def char_val(a):
   sleep(15)
   b(a)

def f(val):
    if isinstance(val, str):
        char_val(val)
        print(datetime.datetime.now())
    else:
        int_val(val)
        print(datetime.datetime.now())

list_val = [1,2,'c',6,10,1,'e',11,78,'a', 'b']

if __name__ == '__main__':
   p = multiprocessing.Pool(4)
   p.map(f, list_val)
   # for index, val in enumerate(list_val):
      # if isinstance(val, str):
         # p.map(char_val, [val])
         # print(datetime.datetime.now())
      # else:
         # p.map(int_val, [val])
         # print(datetime.datetime.now())

Output:输出:

1
2020-03-29 11:38:15.373607
2
2020-03-29 11:38:15.373764
6
2020-03-29 11:38:15.374008
10
1
2020-03-29 11:38:15.374117
2020-03-29 11:38:15.374108
11
2020-03-29 11:38:15.374233
78
2020-03-29 11:38:15.374438
c
a
2020-03-29 11:38:30.388652
2020-03-29 11:38:30.388761
e
b
2020-03-29 11:38:30.389465
2020-03-29 11:38:30.389566

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM