-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathprocesses_barrier.py
More file actions
36 lines (29 loc) · 1.08 KB
/
Copy pathprocesses_barrier.py
File metadata and controls
36 lines (29 loc) · 1.08 KB
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
import multiprocessing
from multiprocessing import Barrier, Lock, Process
from time import time
from datetime import datetime
def test_with_barrier(synchronizer, serializer):
name = multiprocessing.current_process().name
synchronizer.wait()
now = time()
with serializer:
print("process %s ----> %s" \
%(name,datetime.fromtimestamp(now)))
def test_without_barrier():
name = multiprocessing.current_process().name
now = time()
print("process %s ----> %s" \
%(name ,datetime.fromtimestamp(now)))
if __name__ == '__main__':
synchronizer = Barrier(2)
serializer = Lock()
Process(name='p1 - test_with_barrier'\
,target=test_with_barrier,\
args=(synchronizer,serializer)).start()
Process(name='p2 - test_with_barrier'\
,target=test_with_barrier,\
args=(synchronizer,serializer)).start()
Process(name='p3 - test_without_barrier'\
,target=test_without_barrier).start()
Process(name='p4 - test_without_barrier'\
,target=test_without_barrier).start()