-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMy Threads.py
More file actions
81 lines (68 loc) · 2.12 KB
/
My Threads.py
File metadata and controls
81 lines (68 loc) · 2.12 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# Here is how I am trying to understand what threading
# is all about. It seems as if each def function runs each
# threaded instruction starting from the top downward
# on each of the def function's instructions. The delay(n)
# function shows how the threads work, as if looking at
# them in a slow motion time lapse to show how they work
# with each def function call thread. Threading gives the
# illusion that each of the four def functions are running at
# the same time, when really, they aren't.
from time import sleep as delay;import threading
def function1():
print('print commands 1')
delay(1)
print('print commands 2')
delay(1)
print('print commands 3')
delay(1)
print('print commands 4')
delay(1)
def function2():
print('print commands 1')
delay(1)
print('print commands 2')
delay(1)
print('print commands 3')
delay(1)
print('print commands 4')
delay(1)
def function3():
print('print commands 1')
delay(1)
print('print commands 2')
delay(1)
print('print commands 3')
delay(1)
print('print commands 4')
delay(1)
def function4():
print('print commands 1')
delay(1)
print('print commands 2')
delay(1)
print('print commands 3')
delay(1)
print('print commands 4')
delay(1)
# Call the thread functions example 1:
threading.Thread(target=function1).start()
threading.Thread(target=function2).start()
threading.Thread(target=function3).start()
threading.Thread(target=function4).start()
# Call the thread functions example 2:
a=threading.Thread(target=function1)
b=threading.Thread(target=function2)
c=threading.Thread(target=function3)
d=threading.Thread(target=function4)
a.start()
b.start()
c.start()
d.start()
# For-loop example:
my_threads=(
threading.Thread(target=function1),
threading.Thread(target=function2),
threading.Thread(target=function3),
threading.Thread(target=function4))
for i in my_threads:
i.start()