pluaron
2024-05-28

python并行

Pparallelism

python并行执行任务

python 执行shell

1
2
3
4
5
6
7
8

import subprocess as subp

def Subp_call(cmd):
print(cmd)
subp.check_call(cmd, shell=True)
return

python 异步进程池

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

from multiprocessing import Pool
import time

n_threads = 5

params_list = [[], []]

def Work(i):
time.spleep(2)
return

def Multi_process(function, params_list, n_threads):

pool = Pool(n_threads)
for param in params_list:
pool.apply_async(func=function, args=param)
pool.close()
pool.join()

return

python 异步进程池,并获取返回结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

from multiprocessing import Pool
import time

n_threads = 5

params_list = [[], []]

def Work(i):
time.spleep(2)
return i

def Multi_process_get(function, params_list, n_threads):

res_list = []
pool = Pool(n_threads)
for param in params_list:
res = pool.apply_async(func=function, args=param)
res_list.append(res)
pool.close()
pool.join()

out_list = []
for res in res_list:
out_list.append(res.get())

return out_list

python 异步进程池,并同时打印进程进度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

from multiprocessing import Pool
import time
from tqdm import tqdm

n_threads = 5

params_list = [[], []]

def Work(i):
time.spleep(2)
return i

def Multi_process(function, params_list, n_threads, description=""):

pbar = tqdm(total=len(params_list))
pbar.set_description(description)
update = lambda *args: pbar.update()

pool = Pool(n_threads)
for param in params_list:
pool.apply_async(function, param, callback=update)
pool.close()
pool.join()

return

python 异步进程池,获取返回结果, 并同时打印进程进度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

from multiprocessing import Pool
import time
from tqdm import tqdm

n_threads = 5

params_list = [[], []]

def Work(i):
time.spleep(2)
return i

def Multi_process(function, params_list, n_threads, description=""):

pbar = tqdm(total=len(params_list))
pbar.set_description(description)
update = lambda *args: pbar.update()


res_list = []
pool = Pool(n_threads)
for param in params_list:
res = pool.apply_async(func=function, args=param, callback=update)
res_list.append(res)
pool.close()
pool.join()

out_list = []
for res in res_list:
out_list.append(res.get())

return out_list