Python - SPLessons

Python Multithreaded Programming

Home > Lesson > Chapter 14
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Python Multithreaded Programming

Python Multithreaded Programming

shape Description

Multiple threads inside a procedure have similar information space with the fundamental string and can hence impart data or convey to each other more effortlessly than if they were separate procedures. Strings here and there called light-weight procedures and they don't require much memory overhead; they are less expensive than procedures. A string has a starting, an execution grouping, and a conclusion. It has a direction pointer that monitors where inside its setting it is right now running.

Starting a New Thread

shape Description

The following is the method to start a thread. [c]thread.start_new_thread ( function, args[, kwargs] )[/c] This strategy call empowers a quick and productive approach to make new strings in both Linux and Windows. The strategy call returns promptly and the kid string begins and calls work with the passed rundown of args. At the point when capacity gives back, the string ends. The following is an example. [c]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()) ) # Create two threads as follows 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[/c] Now compile the code result will be as follows. [c]Thread-1: Thu Dec 22 16:07:56 2016 Thread-2: Thu Dec 22 16:07:58 2016 Thread-1: Thu Dec 22 16:07:58 2016 Thread-1: Thu Dec 22 16:08:00 2016 Thread-2: Thu Dec 22 16:08:02 2016 Thread-2: Thu Dec 22 16:08:06 2016 Thread-2: Thu Dec 22 16:08:10 2016[/c]

Synchronization Of Thread

shape Description

The threading module furnished with Python incorporates an easy to-actualize locking component that permits you to synchronize strings. Another bolt is made by calling the Lock() technique, which gives back the new bolt. The acquire(blocking) strategy for the new bolt protest is utilized to constrain strings to run synchronously. The discretionary blocking parameter empowers you to control whether the string holds up to procure the bolt. In the event that blocking is set to 0, the string returns quickly with a 0 esteem if the bolt can't be gained and with a 1 if the bolt was procured. In the event that blocking is set to 1, the string pieces and sit tight for the bolt to be discharged. The following is an example. [c]import threading import time class myThread (threading.Thread): def __init__(self, threadID, name, counter): threading.Thread.__init__(self) self.threadID = threadID self.name = name self.counter = counter def run(self): print "Starting " + self.name # Get lock to synchronize threads threadLock.acquire() print_time(self.name, self.counter, 3) # Free lock to release next thread threadLock.release() def print_time(threadName, delay, counter): while counter: time.sleep(delay) print "%s: %s" % (threadName, time.ctime(time.time())) counter -= 1 threadLock = threading.Lock() threads = [] # Create new threads thread1 = myThread(1, "Thread-1", 1) thread2 = myThread(2, "Thread-2", 2) # Start new Threads thread1.start() thread2.start() # Add threads to thread list threads.append(thread1) threads.append(thread2) # Wait for all threads to complete for t in threads: t.join() print "Exiting Main Thread"[/c] Now compile the code result will be as follows. [c]Starting Thread-1Starting Thread-2 Thread-2: Thu Dec 22 16:12:13 2016 Thread-2: Thu Dec 22 16:12:15 2016 Thread-2: Thu Dec 22 16:12:17 2016 Thread-1: Thu Dec 22 16:12:18 2016 Thread-1: Thu Dec 22 16:12:19 2016 Thread-1: Thu Dec 22 16:12:20 2016 Exiting Main Thread[/c]

Summary

shape Key Points

  • The put() is the method to add an item into an queue.
  • The get() is the method to get and return an item.