Skip to main content

Python Multithreaded Programming

Python Multithreaded Programming
When programmers run a simple program of Python, execution starts at the first line and proceeds line-by-line. Also, functions & loops may be the reason of program execution to jump but it is fairly easy to see its working procedures and which line will be next executed. Programmers can put their fingers and can trace the lines of codes that will be executed; this is called single threaded programming.

However, in case of multi-threaded programming, it’s like putting a second finger on your program. Both the fingers move the same way and will be executed simultaneously.

What Are Threads?

It is the execution of tiny sequence of program instruction that can be managed independently and is a distinctive part of operating system. Modern OS manage multiple programs using time-sharing technique. In Python, there are two different kinds of thread. These are:
  • Kernel Threads
  • User-space threads or User threads

Why To Use Thread

Thread plays a major role in application programming. All the GUI programs and web servers are threaded together. The main reasons of using threads are:
  • Parallel Computation: If any user has multiprocessor machine then thread can allow to do parallel processing with the goal of increase in processing speed.
  • Standardization: It became a standard approach for all programming languages as it increases programming speed.
  • Parallel I/O (Input/Output): When we talk about the speed of input & output, it is comparatively slow in CPU. By placing each i/o operations in multiple individual threads, programmers can make use of operations done in parallel with each other & with the computation speed.
  • Asynchronous Events: Multiple threaded applications can deal with asynchronous actions. For example in a program, programmers may don’t know whether the next action will be to use the mouse or to use the keyboard. By planting a separate thread for each action i.e. two threads both for mouse and keyboard, programmers can able to code a cleaner, efficient application which is to use non-blocking I/O operations.

Thread Modules In Python

There are two ways for accessing Python threads. These are by using:
  • py module
  • py module
It is to be noted that ‘tread’ module has been considered as of lesser use and hence users get to use the ‘threading’ module instead. Another thing has to keep in mind that the module ‘thread’ treats the thread like a function whereas the ‘threading’ is implemented as an object.

Benefits Of Threading

  • For a single process, multiple threads can be used to process and share the same data-space and can communicate with each other by sharing information.
  • They use lesser memory overhead and hence they are called lightweight processes.
  • A program can remain responsive to input when threads are used.
  • Threads can share and process the global variable’s memory.
In a thread, there are three different parts. It has the beginning, an execution part and a conclusion. It also has an instruction pointer that points to where the thread or process is currently running and hence the thread is able to run several different program blocks concurrently.

Using A New Thread

It is achievable to execute functions in a separate thread using a module Thread. For doing this, programmers can use the function – thread.start_new_thread().
Syntax:
thread.start_new_thread(function, args[, kwargs])
Here, the first part is a method as told before & this method is a faster and more efficient way to create new threads. As the child thread starts the function passes list of args. The thread gets terminated when the function returns a value. The ‘args’ in the above syntax is a tuple of arguments.

Program Of Threading Using Python

Example:
#!/usr/bin/python

import threading

def coder(number):
print ' Coders: %s' , %number
return

threads
= []
for k in range(5):
t
= threading.Thread(target=coder, args=(k,))
threads
.append(t)
t
.start()
Output:
Coders: 0

Coders: 1

Coders: 2

Coders: 3

Coders: 4

Methods Of Thread Class

The threading module, as described earlier has a Thread class that is used for implementing threads and that class also contains some predefined methods used by programmers in multi-threaded programming. These are:
  • run(): It acts as the entry of the thread
  • start(): is used for starting the thread by calling the run()
  • isAlive(): is used to verify whether the still executing or not
  • getName(): is used for returning the name of a thread
  • setName(): is used to set the name of the thread


Comments

For Programs Click Here

Popular posts from this blog

Syllabus

Syllabus  C Programming Tutorials C Tutorial C Introduction History of C Programming Language C Installation C Program Structure C Input and Output (I/O) C Format Specifiers Declaration & Assignments C Tokens C Identifiers C Keywords C Constants C Operators C Data Types C Variables C Preprocessors C Type Casting C Custom Header File Flow Control C Decision Making C if Statements C if-else Statements C Nested if-else Statements C else-if Statements C goto Statement C switch Statements C Loops C while loops C do while loops C for loops Functions C Functions C Function Arguments C Library Functions C Variable Scope Arrays & Strings C Arrays C Strings Pointers C Pointers C Dynamic Memory Allocation Structure & Union C Structures C Unions File I/O C File Handling C fopen C fclose C getc C putc C getw C putw C fprintf C fscanf C fgets C fputs C feof                                     ...

Syllabus

Python Tutorials Python Tutorial Python Overview Python Installation Basics of Python Programming Python Operators Python Keywords Python Numbers Python Strings Python Data Types Python Variables Python Lists Python Tuples Python Date and Time Python Decision Making Python Loops Python File Handling Python Dictionaries Python Functions Python Modules Python Exceptions Handling Python Object Oriented Inheritance in Python Python Regular Expressions Python Networking Programming Python Multithreaded Programming Python CGI Programming Python Database Connection Python Metaprogramming Python Data Processing And Encoding Python GUI Programming

C++ Data Abstraction

C++ Data Abstraction Object oriented programming offers various features to write programs with various concepts that help to minimize problems and increase flexibility in program. One of the features of object oriented programming is Data abstraction. In this chapter you will learn about how the concept data abstraction is carried out within the C++ program. What is Data abstraction? Data abstraction allows a program to ignore the details of how a data type is represented. Abstraction (derived from a Latin word  abs , meaning away from and  trahere , meaning to draw) refers to the act of representing essential features without including the background details or explanations. C++ classes use the technique of abstraction and are defined as a list of abstract attributes such as width, cost, size etc and functions to operate on these attributes. They put in a nutshell all the essential properties of an object that are required to be created. The attributes are therefore called...