Repository navigation
Expand file tree
/
Copy path010-python-multithreading.py
More file actions
149 lines (123 loc) · 5.36 KB
/
Copy path010-python-multithreading.py
File metadata and controls
149 lines (123 loc) · 5.36 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
""" MARKDOWN
---
YamlDesc: CONTENT-ARTICLE
Title: Python Modules Threading
MetaDescription: Python Modules, Threading, _thread
MetaKeywords: Python Modules, Threading, _thread
Author: Venkata Bhattaram / tinitiate.com
ContentName: python-modules-threading
---
MARKDOWN """
""" MARKDOWN
# PYTHON MULTITHREADING PARALLEL / CONCURRENT PROGRAMMING
* PYTHON supports Multithreading
* Normal execution of python, where each step in the code is executed one after
another is run as a single THREAD. but in a multi-threaded application, code
(or steps) can be run together with another in parallel (two or more steps at
the same time) even though the code is written one after another.
* Such a requirement is very much needed in a variety of applications, such as
games, GUI applications, large volume data processing.
* To invoke multithreading features we need to use the threading module
* Here we demonstrate multithreading by
* Running a function that takes 5 seconds to complete execution using a SLEEP
command.
* We run the function TWICE and it takes 10 Secs to complete the execution in
sequential execution.
* But when we run the same function in TWO threads parallelly then it takes
only 5 secs to run both the functions at the same time concurrently.
MARKDOWN """
""" MARKDOWN
# CREATE THREADS USING THE _THREAD MODULE
* USING the _thread.start_new_thread we can pass any function and its parameters
that we want to run as a parallel thread.
* This function accepts TWO parameters (1) Target function (2) List of function
parameters
* In this case we want to run the function: five_seconds_process
and its SINGLE parameter. Also make sure to include a "COMMA" after the
LAST PARAMETER that is being passed to the _thread.start_new_thread
MARKDOWN """
# MARKDOWN ```
import _thread
import time
# Create a FUNCTION that takes FIVE seconds to execute (This is a wait (sleep) command
# that pauses the program for FIVE seconds
def five_seconds_process(thread_id):
"This function when called takes 5 Seconds to execute"
# Thread Start message
print("ThreadID: ",thread_id," Started at: ", time.time())
for c in range(5):
print("thread_id: ", thread_id," Iteration Number ", c+1)
time.sleep(1) # This causes the program to PAUSE for 1 Sec
# Thread END message
print("ThreadID: ",thread_id," Ended at: ", time.time())
# ----
print("Demonstration of THREADS using the _thread module's start_new_thread function")
# Run five_seconds_process function as a THREAD with a THREAD-ID 1
_thread.start_new_thread(five_seconds_process,(1,))
# Run five_seconds_process function as a THREAD with a THREAD-ID 2
_thread.start_new_thread(five_seconds_process,(2,))
# This is needed, because all the threads will exit, if the main program finishes
# in order to keep the program alive we make the program execute for 5 seconds
# This infact is running 3 Threads ! (Including the time.sleep(5) below.
time.sleep(5)
# MARKDOWN ```
""" MARKDOWN
# CREATE THREADS USING THE THREADING MODULE
* The **Thread** class from the **threading** module is another and preferred
way to create threads.
* The Thread CLASS constructor accepts a function and its argument list as
a tuple.
* When the OBJECT.start() function is called, it runs as a thread.
MARKDOWN """
# MARKDOWN ```
#Import the threading module
from threading import Thread
# Create a FUNCTION that takes FIVE seconds to execute (This is a wait (sleep) command
# that pauses the program for FIVE seconds
def five_seconds_process(thread_id):
"This function when called takes 5 Seconds to execute"
# Thread Start message
print("ThreadID: ",thread_id," Started at: ", time.time())
for c in range(5):
print("thread_id: ", thread_id," Iteration Number ", c+1)
time.sleep(1) # This causes the program to PAUSE for 1 Sec
# Thread END message
print("ThreadID: ",thread_id," Ended at: ", time.time())
# Create a Thread Object using the Thread class of the threading module
t1 = Thread(target=five_seconds_process, args=(1,))
t1.start()
# Create one more Thread Object using the Thread class of the threading module
t2 = Thread(target=five_seconds_process, args=(2,))
t2.start()
# MARKDOWN ```
""" MARKDOWN
# CREATE DYNAMIC NUMBER THREADS
* Sometimes the number of threads that are needed might depend on volume of
process, for such scenarios we can dynamically create threads, using a loop.
* Here we demonstrate creating THREE threads using a for loop and run the same
function five_seconds_process THRICE using a thread each.
MARKDOWN """
# MARKDOWN ```
from threading import Thread
# Create a FUNCTION that takes FIVE seconds to execute (This is a wait (sleep) command
# that pauses the program for FIVE seconds
def five_seconds_process(thread_id):
"This function when called takes 5 Seconds to execute"
# Thread Start message
print("ThreadID: ",thread_id," Started at: ", time.time())
for c in range(5):
print("thread_id: ", thread_id," Iteration Number ", c+1)
time.sleep(1) # This causes the program to PAUSE for 1 Sec
# Thread END message
print("ThreadID: ",thread_id," Ended at: ", time.time())
# Create a Threads List
threads = []
# Create 3 Threads
for threadID in range(2):
t = Thread(target=five_seconds_process, args=(threadID+1, ))
t.start()
# threads.append(t)
# join all threads
for t in threads:
t.join()
# MARKDOWN ```