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

时间:2020-03-29 05:54:07

标签: python multiprocessing

最近,我一直在尝试使用多处理模块。我写了这个脚本来测试它:

public static void changePassword(String dataFilePath, String oldPassword, String newPassword) {
    ArrayList<String> modList = new ArrayList<>();
    // Create a File object
    File dataFile = new File(dataFilePath);

    // Does the file exist?
    if (!dataFile.exists()) {
        // No - Inform and get outta here!
        System.err.println("File Not Found Error!! (" + dataFilePath + ")");
        return;
    }

    // File exists so...Get Old Password:
    String info = "";
    // 'Try With Resources' used here to auto-close the reader.
    try (BufferedReader bufferedReader = new BufferedReader(new FileReader(dataFile))) {
        while ((info = bufferedReader.readLine()) != null) {
            if (info.equals(oldPassword)) {
                info = newPassword;
            }
            modList.add(info);
        }
    }
    catch (FileNotFoundException ex) {
        System.err.println(ex.getMessage());
    }
    catch (IOException ex) {
        System.err.println(ex.getMessage());
    }
    String[] geg = modList.toArray(new String[0]);

    // Re-write data file with new Password.
    // 'Try With Resources' used here to auto-close writer.
    try (FileWriter f = new FileWriter(dataFile)) {
        for (String dataString : geg) {
            f.write(dataString + System.lineSeparator());
        }
        System.out.println("Password Successfuly Changed!");
    }
    catch (IOException ex) {
        System.err.println(ex.getMessage());
    }
}

输出看起来像这样:

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())

如果看到输出,则该程序尚未真正实现我想要的多重处理。

我希望程序在等待元素“ c”等待15秒时,将处理其他元素,并且几乎所有字符将同时打印。

这可能是一个愚蠢的问题,但我真的迷路了!

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

您要为列表中的每个元素迭代地调用p.map,因此实质上,您正在为每个元素产生一个新的过程。由于p.map是阻塞调用,因此实际上它在继续执行之前先等待char_val('c')的执行。

如果将整个list_val赋予p.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())

输出:

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