File tree 1 file changed +53
-0
lines changed
1 file changed +53
-0
lines changed Original file line number Diff line number Diff line change
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Simple study related to Multiprocessing
4
+
5
+ """
6
+
7
+ import time
8
+ import random
9
+ import threading
10
+
11
+ parallel = random .choice (['True' , 'False' ])
12
+
13
+ counter = 0
14
+ max_for = 100000000
15
+
16
+ lock = threading .Lock ()
17
+
18
+ def add_numbers ():
19
+
20
+ global counter
21
+ for _ in range (0 , max_for ):
22
+ lock .acquire ()
23
+ counter += 1
24
+ lock .release ()
25
+
26
+
27
+ if __name__ == '__main__' :
28
+
29
+ start_time = time .time ()
30
+
31
+ if parallel is True :
32
+
33
+ t1 = threading .Thread (target = add_numbers )
34
+ t2 = threading .Thread (target = add_numbers )
35
+
36
+ t1 .start ()
37
+ t2 .start ()
38
+
39
+ t1 .join ()
40
+ t2 .join ()
41
+
42
+ else :
43
+
44
+ add_numbers ()
45
+ add_numbers ()
46
+
47
+ end_time = time .time () - start_time
48
+
49
+ print (end_time , parallel )
50
+
51
+ print (counter )
52
+
53
+ # assert max_for == max_for*2
You can’t perform that action at this time.
0 commit comments