Starting A New Thread: Example
Starting A New Thread: Example
Multiple threads within a process share the same data space with the main
thread and can therefore share information or communicate with each other
more easily than if they were separate processes.
Threads sometimes called light-weight processes and they do not require much
memory overhead; they are cheaper than processes.
It can temporarily be put on hold (also known as sleeping) while other threads
are running - this is called yielding.
This method call enables a fast and efficient way to create new threads in
both Linux and Windows.
The method call returns immediately and the child thread starts and calls
function with the passed list of args. When function returns, the thread
terminates.
Example
#!/usr/bin/python
import thread
import time
# Define a function for the thread
count = 0
time.sleep(delay)
count += 1
try:
except:
while 1:
pass
Although it is very effective for low-level threading, but the thread module
is very limited compared to the newer threading module.
In addition to the methods, the threading module has the Thread class that
implements threading. The methods provided by the Thread class are as
follows −
start() − The start() method starts a thread by calling the run method.
Then, override the run(self [,args]) method to implement what the thread
should do when started.
Once you have created the new Thread subclass, you can create an
instance of it and then start a new thread by invoking the start(), which in
turn calls run() method.
Example
#!/usr/bin/python
import threading
import time
exitFlag = 0
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
def run(self):
print_time(self.name, 5, self.counter)
while counter:
if exitFlag:
threadName.exit()
time.sleep(delay)
counter -= 1
thread1.start()
thread2.start()
print "Exiting Main Thread"
Synchronizing Threads
The threading module provided with Python includes a simple-to-implement
locking mechanism that allows you to synchronize threads. A new lock is
created by calling the Lock() method, which returns the new lock.
The release() method of the new lock object is used to release the lock
when it is no longer required.
Example
#!/usr/bin/python
import threading
import time
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
def run(self):
threadLock.acquire()
print_time(self.name, self.counter, 3)
threadLock.release()
while counter:
time.sleep(delay)
counter -= 1
threadLock = threading.Lock()
threads = []
thread1.start()
thread2.start()
threads.append(thread1)
threads.append(thread2)
# Wait for all threads to complete
for t in threads:
t.join()
get() − The get() removes and returns an item from the queue.
qsize() − The qsize() returns the number of items that are currently in the
queue.
Example
#!/usr/bin/python
import Queue
import threading
import time
exitFlag = 0
self.threadID = threadID
self.name = name
self.q = q
def run(self):
process_data(self.name, self.q)
queueLock.acquire()
if not workQueue.empty():
data = q.get()
queueLock.release()
else:
queueLock.release()
time.sleep(1)
queueLock = threading.Lock()
workQueue = Queue.Queue(10)
threads = []
threadID = 1
thread.start()
threads.append(thread)
threadID += 1
queueLock.acquire()
workQueue.put(word)
queueLock.release()
pass
exitFlag = 1
for t in threads:
t.join()