-
Notifications
You must be signed in to change notification settings - Fork 0
/
68_multithreading.py
50 lines (36 loc) · 1.24 KB
/
68_multithreading.py
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
# ******************************************************
# Python threading tutorial
# ******************************************************
# thread = a flow of execution. Like a separate order of instructions.
# However each thread takes a turn running to achieve concurrency
# GIL = (global interpreter lock),
# allows only one thread to hold the control of the Python interpreter at any one time
# cpu bound = program/task spends most of it's time waiting for internal events (CPU intensive)
# use multiprocessing
# io bound = program/task spends most of it's time waiting for external events (user input, web scraping)
# use multithreading
import threading
import time
def eat_breakfast():
time.sleep(3)
print("you eat breakfast")
def drink_coffee():
time.sleep(4)
print("you drank coffee")
def study():
time.sleep(5)
print("you finished study")
x = threading.Thread(target=eat_breakfast, args=())
x.start()
y = threading.Thread(target=drink_coffee, args=())
y.start()
z = threading.Thread(target=study, args=())
z.start()
x.join()
y.join()
# eat_breakfast()
# drink_coffee()
# study()
print(threading.active_count())
print(threading.enumerate())
print(time.perf_counter())