A simple code
import _thread
import time
# Define a function for the thread
def print_time( threadName, delay):
count = 0
while count < 5:
time.sleep(delay)
count += 1
print ("%s: %s" % ( threadName,
time.ctime(time.time()) ))
Creating the thread
try:
_thread.start_new_thread( print_time, ("Thread-1",
2, ) )
_thread.start_new_thread( print_time, ("Thread-2",
4, ) )
except:
print ("Error: unable to start thread")
while 1:
pass
The Threading Module:
threading.activeCount(): Returns the
number of thread objects that are
active.
threading.currentThread(): Returns the
number of thread objects in the caller's
thread control.
threading.enumerate(): Returns a list of
all thread objects that are currently
active.
run(): The run() method is the entry
point for a thread.
start(): The start() method starts a
thread by calling the run method.
join([time]): The join() waits for threads
to terminate.
isAlive(): The isAlive() method checks
whether a thread is still executing.
getName(): The getName() method
returns the name of a thread.
setName(): The setName() method sets
the name of a thread.
Creating Thread
using Threading Module:
Define a new subclass of
the Thread class.
Override the __init__(self
[,args]) method to add additional
arguments.
Then override the run(self [,args])
method to implement what the thread
should do when started.
Multithreaded Priority Queue
get(): The get() removes and returns an item
from the queue.
put(): The put adds item to a queue.
qsize() : The qsize() returns the number of
items that are currently in the queue.
empty(): The empty( ) returns True if queue
is empty; otherwise, False.
full(): the full() returns True if queue is full;
otherwise, False.