-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path16_multithreading.py
More file actions
62 lines (45 loc) · 1.29 KB
/
Copy path16_multithreading.py
File metadata and controls
62 lines (45 loc) · 1.29 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from threading import Thread, Lock, current_thread
from queue import Queue
import os
import time
import random
num_threads = os.cpu_count() * 2
print('Lock based threading')
database_value = 0
def increase(lock):
with lock:
global database_value
local_copy = database_value
local_copy += 1
time.sleep(random.uniform(0.1, 0.5))
database_value = local_copy
if __name__ == '__main__':
threads = []
print(f'Start value: {database_value}')
lock = Lock()
for i in range(num_threads):
thread = Thread(target=increase, args=(lock,))
threads.append(thread)
for thread in threads:
thread.start()
for thread in threads:
thread.join()
print(f'End lock based threading with value: {database_value}')
print('Threading queue')
def worker(q, lock):
while True:
value = q.get() # threadsafe
with lock:
print(f'Working in {current_thread().name} got {value}')
q.task_done()
if __name__ == '__main__':
q = Queue()
lock = Lock()
for _ in range(num_threads):
thread = Thread(target=worker, args=(q,lock))
thread.daemon = True
thread.start()
for i in range(1, 21):
q.put(i) # threadsafe
q.join()
print(f'Threading queue finished')