forked from kongdd/python_class
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallelize.py
More file actions
37 lines (31 loc) · 863 Bytes
/
parallelize.py
File metadata and controls
37 lines (31 loc) · 863 Bytes
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
36
37
from multiprocessing import Pool
def job(num):
return num * 2
if __name__ == '__main__':
p = Pool(processes=100)
data = p.map(job, [i for i in range(100)])
p.close()
print(data)
import time
import multiprocessing
def basic_func(x):
if x == 0:
return 'zero'
elif x%2 == 0:
return 'even'
else:
return 'odd'
def multiprocessing_func(x):
y = x*x
time.sleep(2)
print('{} squared results in a/an {} number'.format(x, basic_func(y)))
if __name__ == '__main__':
starttime = time.time()
processes = []
for i in range(0,10):
p = multiprocessing.Process(target=multiprocessing_func, args=(i,))
processes.append(p)
p.start()
for process in processes:
process.join()
print('That took {} seconds'.format(time.time() - starttime))