-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultithreading_worker_counter.py
More file actions
29 lines (22 loc) · 967 Bytes
/
multithreading_worker_counter.py
File metadata and controls
29 lines (22 loc) · 967 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
import concurrent.futures
import os
import threading as th
# A global variable and a lock to control access to this variable
counter = 0
lock = th.Lock()
def worker():
global counter
with lock: # Ensure thread-safe modification of the shared counter
print(f"Counter thread running - Thread: {th.current_thread().name} - ProcessID: {os.getpid()} ")
for _ in range(1000000): # Simulate some work
counter += 1
def run_workers():
with concurrent.futures.ThreadPoolExecutor(max_workers=2) as pool:
futures = [pool.submit(worker) for _ in range(2)]
# Wait for all futures to complete
concurrent.futures.wait(futures)
print(f"Main thread continuing to run - Thread: {th.current_thread().name} - ProcessID: {os.getpid()} ")
return counter
if __name__ == "__main__":
run_workers()
print(f"Final counter value: {counter} - Thread: {th.current_thread().name} - ProcessID: {os.getpid()} ")