forked from PacktPublishing/AdvancedPythonProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocesses.py
More file actions
31 lines (23 loc) · 695 Bytes
/
processes.py
File metadata and controls
31 lines (23 loc) · 695 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
import multiprocessing
import time
class Process(multiprocessing.Process):
def __init__(self, id):
super(Process, self).__init__()
self.id = id
def run(self):
time.sleep(1)
print("I'm the process with id: {}".format(self.id))
def square(x):
return x * x
def map_test():
pool = multiprocessing.Pool()
inputs = [0, 1, 2, 3, 4]
outputs = pool.map(square, inputs)
print(outputs)
outputs_async = pool.map_async(square, inputs)
outputs = outputs_async.get()
print(outputs)
if __name__ == '__main__':
processes = Process(1), Process(2), Process(3), Process(4)
[p.start() for p in processes]
map_test()