-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreading_example.py
More file actions
49 lines (37 loc) · 985 Bytes
/
threading_example.py
File metadata and controls
49 lines (37 loc) · 985 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python3
import os
import threading
import time
TIME_TO_SLEEP = 0.5 # Seconds
NUMBER_OF_THREADS = 4
## Decorator function that adds timing to other functions
def time_me(function):
def inner():
start = time.time()
function()
print(f"The code ran in: {time.time()-start} seconds")
return inner
# Fcuntion to be multithreaded
def threaded_func():
pid = os.getpid()
print(f'Running on core: {pid}.')
for _ in range(10_000_000):
f = (9_999_999.9 * 944_438_468 + 18_468_468)/9_999.26494
time.sleep(TIME_TO_SLEEP)
@time_me
def print_with_threads():
threads_ = []
for _ in range(NUMBER_OF_THREADS):
thread = threading.Thread(target = threaded_func)
threads_.append(thread)
thread.start()
for thread_ in threads_:
thread.join()
@time_me
def print_normal():
for _ in range(NUMBER_OF_THREADS):
threaded_func()
print("Going to try normal printing!")
print_normal()
print("Going to try multi-threaded printing!")
print_with_threads()