Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Python unit 3

The document provides a comprehensive overview of Regular Expressions (RegEx) in Python, detailing the re module and its functions such as search, match, findall, split, and sub. It explains the use of meta characters, special sequences, and provides numerous code examples to illustrate how to implement regex patterns. Additionally, it covers multithreaded programming concepts, including threads, processes, and related modules.

Uploaded by

Kiranmai Konduru
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Python unit 3

The document provides a comprehensive overview of Regular Expressions (RegEx) in Python, detailing the re module and its functions such as search, match, findall, split, and sub. It explains the use of meta characters, special sequences, and provides numerous code examples to illustrate how to implement regex patterns. Additionally, it covers multithreaded programming concepts, including threads, processes, and related modules.

Uploaded by

Kiranmai Konduru
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

For complete python subject tutorials visit : ns lectures youtube channel

UNIT-3
Regular Expressions:

special symbols and characters and functions.

Multithreaded programming:

Threads and processes


Global interpreter lock
Thread module
Threading module
Related modules
For complete python subject tutorials visit : ns lectures youtube channel

Regular Expressions
A Regular Expressions (RegEx) is a special sequence of characters that
uses a search pattern to find a string or set of strings. It can detect the
presence or absence of a text by matching it with a particular pattern, and
also can split a pattern into one or more sub-patterns. Python provides a re
module that supports the use of regex in Python. Its primary function is to
offer a search, where it takes a regular expression and a string. Here, it
either returns the first match or else none.

RegEx Module

Python has a built-in package called re, which can be used to work with
Regular Expressions.

Import the re module:

import re
RegEx in Python

When you have imported the re module, you can start using regular
expressions:

Example-1:

Search the string to see if it starts with "My"and ends with "nagendra":
import re

a = "My name is nagendra"

x = re.search("^My.*nagendra$",a)

if x:

print("YES! We have a match!")

else:

print("No match")
For complete python subject tutorials visit : ns lectures youtube channel

output:

YES! We have a match!

Example-2:
import re
s = 'my name is nagendra'
print(re.search('nagendra', s))

output:

<re.Match object; span=(11, 19), match='nagendra'>


RegEx Functions
The re module offers a set of functions that allows us to search a string for
a match:

Function Description

findall Returns a list containing all matches

search Returns a Match object, if there is a match anywhere


in the string

split Returns a list where the string has been split at each
match

sub Replaces one or many matches with a string


For complete python subject tutorials visit : ns lectures youtube channel

match Returns a Match object if there is a match starting in


the string

The match() Function


The match() function searches the string for a match, and returns a Match
object if there is a match at the starting of my string.

Example-1:
import re

a = "my name is nagendra, my age is 24"

x = re.match("my", a)

print(x)

Output:

<re.Match object; span=(0, 2), match='My'>

Example-2:
import re

a = "my name is nagendra, my age is 24"

x = re.match("The", a)

print(x)
Output:

None

The search() Function


For complete python subject tutorials visit : ns lectures youtube channel

The search() function searches the string for a match, and returns a Match
object if there is a match.

If there is more than one match, only the first occurrence of the match will
be returned:

Match Object

A Match Object is an object containing information about the


search and the result.

Note: If there is no match, the value None will be returned,


instead of the Match Object.

Example-1:
import re

a = "my name is nagendra, my age is 24"

x = re.search("my", a) #here, my is match object

print(x)

Output:

<re.Match object; span=(0, 2), match='My'>

Example-2:
import re

a = "my name is nagendra, my age is 24"


x = re.search("24", a)

print(x)

Output:

<re.Match object; span=(31, 33), match='24'>


For complete python subject tutorials visit : ns lectures youtube channel

The findall() Function


The findall() function returns a list containing all matches.

Example-1:
import re

a = "my name is nagendra, my age is 24"

x = re.findall("My", a)

print(x)

Output:

['my', 'my']

The split() Function


The split() function returns a list where the string has been split at each
match:

Example-1:

import re

a = "my name is nagendra, my age is 24"

x = re.split("nagendra", a)

print(x)

Output:

['my name is ', ', my age is 24']


For complete python subject tutorials visit : ns lectures youtube channel

The sub() Function


The sub() function replaces the matches with the text of your choice:

Example-1:
import re

a = "my name is nagendra, my age is 24"

x = re.sub("nagendra","rahul", a)

print(x)

Output:

my name is rahul, my age is 24

Meta Characters
To understand the RE analogy, Meta Characters are useful, important, and will be
used in functions of module re. Below is the list of meta characters.

Meta Characters Description

Used to drop the special meaning of character


\
following it

[] Represent a character class

^ Matches the beginning

$ Matches the end

. Matches any character except newline


For complete python subject tutorials visit : ns lectures youtube channel

Meta Characters Description

Means OR (Matches with any of the characters


|
separated by it.

? Matches zero or one occurrence

Any number of occurrences (including 0


*
occurrences)

+ One or more occurrences

Indicate the number of occurrences of a preceding


{}
regex to match.

() Enclose a group of Regex

[ ] (Square Brackets)
Square Brackets ([]) represent a character class consisting of a set of
characters that we wish to match. For example, the character class [abc]
will match any single a, b, or c.
We can also specify a range of characters using – inside the square
brackets. For example,
• [0-3] is sample as [0123]
• [a-c] is same as [abc]
We can also invert the character class using the caret(^) symbol. For
example,
• [^0-3] means any number except 0, 1, 2, or 3
• [^a-c] means any character except a, b, or c
example-1:
import re
For complete python subject tutorials visit : ns lectures youtube channel

a= "my name is nagendra"


print(re.findall("[a-m]”, a))
output:
['m', 'a', 'm', 'e', 'i', 'a', 'g', 'e', 'd', 'a']

example-2:
import re
a= "my name is nagendra, my age is 25"
print(re.findall("[0-9]”, a))

output:
['2', '5']

^ (Caret)
Caret (^) symbol matches the beginning of the string i.e. checks whether
the string starts with the given character(s) or not. For example –
• ^B will check if the string starts with g such as Btech, Ball, BOX etc.
• ^BTECH will check if the string starts with BTECH such as BTECH
HYDERABAD, BTECH AIML, BTECH CSE etc.
example-1:
import re
a = 'Btech'
result = re.match(‘^B’, a)

if result:
print("Search successful.")
else:
print("Search unsuccessful.")
For complete python subject tutorials visit : ns lectures youtube channel

output:

Search successful.

example-2:

import re

a = 'Btech hyderabad'

result = re.match(‘^Btech’, a)

if result:

print("Search successful.")

else:

print("Search unsuccessful.")

output:

Search successful.

$ (Dollar)
Dollar($) symbol matches the end of the string i.e checks whether the string
ends with the given character(s) or not. For example –
• s$ will check for the string that ends with a such as geeks, ends, s,
etc.
• ks$ will check for the string that ends with ks such as geeks,
geeksforgeeks, ks, etc.
example-1:
import re
a = 'Btech'
result = re.search(‘h$’, a)
For complete python subject tutorials visit : ns lectures youtube channel

if result:
print("Search successful.")
else:
print("Search unsuccessful.")

output:

Search successful.

example-2:

import re

a = 'Btech hyderabad'

result = re.search(‘hyderabad$’, a)

if result:

print("Search successful.")

else:

print("Search unsuccessful.")

output:

Search successful.

. (Dot)
Dot(.) symbol matches only a single character except for the newline
character (\n). For example –
• a.b will check for the string that contains any character at the place of
the dot such as acb, adb, arb, a1b, etc...
For complete python subject tutorials visit : ns lectures youtube channel

• .. will check if the string contains at least 2 characters.for example


a..b will check for the string that contains any two character at the
place of the dot such as acrb, adhb, arfb, a12b, etc…
example-1:
import re
a= "hello hyderabad"
x = re.findall("he..o", a)
print(x)
output:
['hello']

example-2:
import re
a= "hello hyderabad"
x = re.findall("hyd .... d", a)
print(x)
output:
['hyderabad']

| (Or)
Or symbol works as the or operator meaning it checks whether the pattern
before or after the or symbol is present in the string or not. For example –
• btech|mtech will match any string that contains btech or mtech.

Example-1:
import re
a= "i am from btech and i am from mtech "
For complete python subject tutorials visit : ns lectures youtube channel

x = re.findall("btech|mtech", a)
print(x)
output:
['btech', 'mtech']

Example-2:
import re
a= "i am nagendra and i am from BTECH"
x = re.findall(" BTECH | MTECH ", a)
print(x)
output:
['btech']

? (Question Mark)
The question mark symbol ? matches zero or one occurrence of the
pattern left to it.
Expression String Matched?

mn 1 match

man 1 match

No match (more than one a


ma?n maaan
character)

main No match (a is not followed by n)

woman 1 match

example-1:
For complete python subject tutorials visit : ns lectures youtube channel

import re
a= "i am a man"
x = re.findall("ma?n", a)
print(x)
output:
['man']

example-2:
import re
a= "i am a mn"
x = re.findall("ma?n", a)
print(x)
output:
['mn']

example-3:
import re
a= "i am a maaaan"
x = re.findall("ma?n", a)
print(x)
output:
[ ] ( output is empty because a repeated more than once. The question
mark symbol ? matches zero or one occurrence of the pattern left to it.)

* (Star)
The star symbol * matches zero or more occurrences of the pattern left to
it.
For complete python subject tutorials visit : ns lectures youtube channel

Expression String Matched?

mn 1 match

man 1 match

ma*n maaan 1 match

main No match (a is not followed by n)

woman 1 match

example-1:
import re
a= "i am a maaan"
x = re.findall("ma*n", a)
print(x)
output:
['maaan']
example-2:
import re
a= "i am a mn"
x = re.findall("ma*n", a)
print(x)
output:
['mn']

+ (Plus)
The plus symbol + matches one or more occurrences of the pattern left to it.
For complete python subject tutorials visit : ns lectures youtube channel

Expression String Matched?

mn No match (no a character)

man 1 match

maaan 1 match

ma+n No match (a is not followed


main
by n)

woman 1 match

example-1:
import re
a= "i am a maaan"
x = re.findall("ma+n", a)
print(x)
output:
['maaan']

example-2:
import re
a= "i am a mn"
x = re.findall("ma+n", a)
print(x)

output:
For complete python subject tutorials visit : ns lectures youtube channel

[ ] ( output is empty because a is not present .The plus symbol + matches


one or more occurrences of the pattern left to it)

{ } (Braces)
Consider this code: {n,m}. This means at least n, and at most m repetitions
of the pattern left to it.
Example-1:
import re
a= "my name is nagendra, my age is 25"
x = re.findall("my{1,3}", a)
print(x)

output:
['my', 'my']

from above pattern my{1,3} mean --if “my” is present at least once and
maximum three time then it will print “my”
from above example “my” is present twice, so it will print my twice

( ) -Group
Group symbol is used to group sub-patterns.

List of special sequences


Special sequences do not match for the actual character in the string
instead it tells the specific location in the search string where the match
must occur. It makes it easier to write commonly used patterns.
For complete python subject tutorials visit : ns lectures youtube channel

Special
Description
Sequence

\A Matches if the string begins with the given character

Matches if the word begins or ends with the given character.


\b \b(string) will check for the beginning of the word and (string)\b
will check for the ending of the word.

It is the opposite of the \b i.e. the string should not start or end
\B
with the given regex.

Matches any decimal digit, this is equivalent to the set class [0-
\d
9]

Matches any non-digit character, this is equivalent to the set


\D
class [^0-9]

\s Matches any whitespace character.

\S Matches any non-whitespace character

Matches any alphanumeric character, this is equivalent to the


\w
class [a-zA-Z0-9_].

\W Matches any non-alphanumeric character.

\Z Matches if the string ends with the given regex


For complete python subject tutorials visit : ns lectures youtube channel

Program:
import re
a="my name is nagendra, my age is 25"
b=re.findall("\Amy",a)
c=re.findall("\w",a)
d=re.findall("\W",a)
e=re.findall("\d",a)
f=re.findall("\D",a)
g=re.findall("\s",a)
h=re.findall("\S",a)
i=re.findall(r"\bmy", a)
i=re.findall(r"\bna", a)
j=re.findall(r"ra\b",a)
print("output of \A is ",b)
print("output of \w is ",c)
print("output of \W is ",d)
print("output of \d is ",e)
print("output of \D is ",f)
print("output of \s is ",g)
print("output of \S is ",h)
print("output of \b is ",i)
print("output of \b is ",j)
For complete python subject tutorials visit : ns lectures youtube channel

output:
output of \A is ['my']

output of \w is ['m', 'y', 'n', 'a', 'm', 'e', 'i', 's', 'n', 'a', 'g', 'e', 'n', 'd', 'r', 'a',
'm', 'y', 'a', 'g', 'e', 'i', 's', '2', '5']

output of \W is ['', '', '', ',', '', '', '', '']

output of \d is ['2', '5']

output of \D is ['m', 'y', '', 'n', 'a', 'm', 'e', '', 'i', 's', '', 'n', 'a', 'g', 'e', 'n', 'd',
'r', 'a', ',', '', 'm', 'y', '', 'a', 'g', 'e', '', 'i', 's', '']

output of \s is ['', '', '', '', '', '', '']

output of \S is ['m', 'y', 'n', 'a', 'm', 'e', 'i', 's', 'n', 'a', 'g', 'e', 'n', 'd', 'r', 'a',
',', 'm', 'y', 'a', 'g', 'e', 'i', 's', '2', '5']

output of \b is ['na’]

output of \b is ['ra’]
For complete python subject tutorials visit : ns lectures youtube channel

Multithreading
Python Multithreading:
Multithreading is a threading technique in Python programming to run
multiplethreads concurrently by rapidly switching between threads with
a CPU help (called context switching). Besides, it allows sharing of its
data space with the main threads inside a process that share
information and communication with other threads easier than
individual processes. Multithreading aims to perform multiple tasks
simultaneously, which increases performance, speed and improves the
rendering of the application.
Benefits of Multithreading in Python
A thread is the smallest unit of a program or process executed independently
or scheduled by the Operating System. In the computer system, an Operating
System achieves multitasking by dividing the process into threads. A thread is
a lightweight process that ensures the execution of the process separately on
the system. In Python 3, when multiple processors are running on a program,
each processor runs simultaneously to execute its tasks separately.

Following are the benefits to create a multithreaded application in Python, as


follows:
1. It ensures effective utilization of computer system resources.
2. Multithreaded applications are more responsive.
3. It shares resources and its state with sub-threads (child) which makes it
more economical.
4. It makes the multiprocessor architecture more effective due to similarity.
5. It saves time by executing multiple threads at the same time.
6. The system does not require too much memory to store multiple threads.

When to use Multithreading in Python?


It is a very useful technique for time-saving and improving the performance of
an application. Multithreading allows the programmer to divide application
tasks into sub-tasks and simultaneously run them in a program. It allows
threads to communicate and share resources such as files, data, and
memory to the same processor. Furthermore, it increases the user's
responsiveness to continue running a program even if a part of the application
is the length or blocked.
How to achieve multithreading in Python?
There are two main modules of multithreading used to handle threads in
Python.
For complete python subject tutorials visit : ns lectures youtube channel

1. The thread module


2. The threading module

Thread modules
A thread is an entity within a process that can be scheduled for execution.
Also, it is the smallest unit of processing that can be performed in an OS
(Operating System). In simple words, a thread is a sequence of such
instructions within a program that can be executed independently of other
codes.
It is started with Python 3, designated as obsolete, and can only be
accessed with _thread thatsupports backward compatibility.
Syntax:
thread.start_new_thread ( function_name, args[, kwargs] )
example:
import _thread
import time
def name(n):
time.sleep(0.5)
print("my name is:",n)
def country(m):
time.sleep(0.5)
print("my country is :",m)
r=time.time()
_thread.start_new_thread(name,("nagendrasai",))
_thread.start_new_thread(country,("india",))
print("time taken to execute two functions:",time.time()-r)

output:
my name is nagendrasai
my country is india
time taken to execute two functions:00:0045555
For complete python subject tutorials visit : ns lectures youtube channel

Types of Threads
There are two types of threads, which are:
1. User Level Thread

As the name suggests, the user-level threads are only managed by users,
and the kernel does nothave its information.
These are faster, easy to create and manage.
The kernel takes all these threads as a single process and handles them
as one process only. The user-level threads are implemented by user-
level libraries, not by the system calls.
2. Kernel-Level Thread

The kernel-level threads are handled by the Operating system and managed
by its kernel. These threads are slower than user-level threads because
context information is managed by the kernel. To create and implement a
kernel-level thread, we need to make a system call.
Features of Thread
• Threads share data, memory, resources, files, etc., with their peer threads
within a process.
• One system call is capable of creating more than one thread.
• Each thread has its own stack and register.
• Threads can directly communicate with each other as they share the same
address space.
• Threads need to be synchronized in order to avoid unexpected scenarios.

Threading Modules
The threading module included with Python 2.4 provides much more
powerful, high-level support for threads than the thread module .To use
multithreading, we need to import the threading module in Python Program.
The threading module exposes all the methods of the thread module and
provides some additional methods −
• threading.activeCount() − Returns the number of thread objects that are
active.
• threading.currentThread() − Returns the number of thread objects
in the caller's threadcontrol.
• threading.enumerate() − Returns a list of all thread objects that are
currently active.
In addition to the methods, the threading module has the Thread class that
For complete python subject tutorials visit : ns lectures youtube channel

implements threading. The methods provided by the Thread class are as follows

• run() − The run() method is the entry point for a thread.
• start() − The start() method starts a thread by calling the run method.
• join() − 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.

threading Module Objects:

Apart from the functions specified above, the threading module also provides
many classes whose objects are very useful in creating and managing threads.

Following are some of the Object types:

Object Description

Thread Object that represents a single thread of execution.

Lock Primitive lock object.

RLock or Re-entrant lock object provides ability for a


RLock single thread to (re)acquire an already-held lock
(recursive locking).

Condition variable object causes one thread to wait until


Condition certain "condition" has been satisfied by another thread
(such as change in state or some data value)

Its a more general version of condition variables, whereby


a number of threads can be made to wait for some event
Event
to occur and all the threads waiting will only awaken
when the event happens.

Provides a "counter" of finite resources shared between


Semaphore
threads block when none are available.

BoundedSemaphore Similar to a Semaphore but ensures that it never exceeds


For complete python subject tutorials visit : ns lectures youtube channel

its initial value.

Similar to Thread, except that it waits for a specified


Timer
period of time before running.

Creates a "barrier" at which a specified number of


Barrier threads must all arrive before they're all allowed to
continue.

The above table gives a brief introduction of various object types in python
threading module. We will discuss about all these objects in details in the next few
tutorials.

Follow the given below steps to implement the threading module in Python
Multithreading:
1. Import the threading module:

Create a new thread by importing the threading module, as shown.


Syntax:
import threading
A threading module is made up of a Thread class, which is instantiated to
create a Python thread.
2. Declaration of the thread parameters:

It contains the target function, argument, and kwargs as the parameter in the
Thread() class.
• Target: It defines the fun4. Join method:ction name that is executed by
the thread.
• Args: It defines the arguments that are passed to the target function
name.
3. Start a new thread:

To start a thread in Python multithreading, call the thread class's object. The
start() method can becalled once for each thread object; otherwise, it throws an
exception error.

4. Join method:
It is a join() method used in the thread class to halt the main thread's
execution and waits till the complete execution of the thread object. When the
thread object is completed, it starts the execution of the main thread in
Python.
For complete python subject tutorials visit : ns lectures youtube channel

Multi threading programs:

1. python programming for multi threading in which one thread executes and
prints the square of numbers and other thread executes and prints the cube of
numbers from 1 to 5
(OR)
PYTHON PROGRAM TO ACHIEVE PARALLELISM BY THREADING
MODULE
(OR)
PROGRAM FOR INTER PROCESS COMMUNICATION
(OR)
PYTHON PROGRAM FOR MULTI-THREADING

PROGRAM:

import threading
import time
def square(n):
print("square of a number:")
for i in n:
time.sleep(0.2)
print("square is :",i*i)
def cube(n):
print("cube of a number:")
for i in n:
time.sleep(0.2)
print("cube is :",i*i*i)

r=time.time()
l=[1,2,3,4,5]
t1=threading.Thread(target=square,args=(l,))
t2=threading.Thread(target=cube,args=(l,))
t1.start()
t2.start()
t1.join()
t2.join()
print("time taken to execute two functions:",time.time()-r)output:

square of a number:
cube of a number:
square is : 1
cube is : 1
cube is : 8
square is : 4
cube is : 27
square is : 9
For complete python subject tutorials visit : ns lectures youtube channel

cube is : 64
square is : 16
cube is : 125
square is : 25
time taken to execute two
functions: 1.0623559951782227

2. python program to create two threads. one thread will display the numbers
from 1 to 10 in normal order and other thread will display the numbers in
reverse order

import threading
import time
def normalorder(n):
print("normal order:")
for i in range(1,n+1):
time.sleep(0.2)
print(i)
def reverseorder(n):
print("reverse order:")
for i in range(n,0,-1):
time.sleep(0.2)
print(i)

r=time.time()
t1=threading.Thread(target=normalorder,args=(10,))
t2=threading.Thread(target=reverseorder,args=(10,))
t1.start()
t1.join()
t2.start()
t2.join()
print("time taken to execute two functions:",time.time()-r)

output:

normal order:
1
2
3
4
5
6
7
8
9
For complete python subject tutorials visit : ns lectures youtube channel

10

reverse order:
10
9
8
7
6
5
4
3
2
1
time taken to execute two functions: 4.010312557220459

Difference between Process and Thread


Both process and thread are related to each other and quite similar as these are the
independent sequence of execution. The basic difference between a process and a
thread is that a process takes place in different memory spaces, whereas a thread
executes in the same memory space.

A process is an instance of a program that is being executed. When we run a


program, it does not execute directly. It takes some time to follow all the steps
required to execute the program, and following these execution steps is known as a
process.

A thread is the subset of a process and is also known as the lightweight


process. A process can have more than one thread, and these threads are
managed independently by the scheduler. All the threads within one process
are interrelated to each other.

S.NO Proces Thread


s
Process means any program
1. Thread means a segment of a process.
is inexecution.
The process takes more
2. The thread takes less time to terminate.
time toterminate.
3. It takes more time for creation. It takes less time for creation.
It also takes more time for
4. It takes less time for context switching.
contextswitching.
The process is less
5. Thread is more efficient in terms of communication.
efficient interms of
communication.
We don’t need multi programs in action for
Multiprogramming holds
6. multiple threads because a single process
theconcepts of multi-
consists of multiplethreads.
process.
For complete python subject tutorials visit : ns lectures youtube channel

7. The process is isolated. Threads share memory.


The process is called A Thread is lightweight as each thread in a
8.
theheavyweight processshares code, data, and resources.
process.
Process switching uses an Thread switching does not require calling an
9.
interface in an operating operatingsystem and causes an interrupt to the
system. kernel.
If one process is blocked
If a user-level thread is blocked, then all other user-
10. then itwill not affect the
levelthreads are blocked.
execution of other
processes
The process has its own
Thread has Parents’ PCB, its own Thread Control
11. Process Control Block, Stack,
Block,and Stack and common Address space.
and AddressSpace.
Since all threads of the same process share
Changes to the parent address spaceand other resources so any changes
12.
process donot affect child to the main thread may affect the behavior of the
processes. other threads of the process.
13. A system call is involved in it. No system call is involved, it is created using APIs.
The process does not share
14. datawith each other. Threads share data with each other.

Note: In some cases where the thread is processing a bigger workload


compared toa process’s workload then the thread may take more time
to terminate. But this is an extremely rare situation and has fewer
chances to occur.

Inter process Communication (IPC)


IPC is a mechanism which allows the exchange of data between processes. It
enables resource and data sharing between the processes without interference.
Processes that execute concurrently in the operating system may be either
independent processes or cooperating processes.
A process is independent and it may or may not be affected by other processes
executing in the system. Any process that does not share data with any other
process is independent.
Suppose if a process is cooperating then, it can be affected by other processes
that are executing in the system. Any process that shares the data with another
process is called a cooperative process.
Given below is the diagram of inter process communication −
For complete python subject tutorials visit : ns lectures youtube channel

Reasons for Process Cooperation


There are several reasons which allow process cooperation, which is as follows −
• Information sharing − Several users are interested in the same piece of
information. We must provide an environment to allow concurrent access to
such information.
• Computation speeds up − If we want a particular task to run faster, we must
break it into subtasks, then each will be executed in parallel with the other.
The speedup can be achieved only if the computer has multiple processing
elements.
• Modularity − A system can be constructed in a modular fashion dividing the
system functions into separate processes or threads.
• Convenience − An individual user may work on many tasks at the same time.
For example, a user may be editing, compiling, and printing in parallel.
The cooperative process requires an IPC mechanism that will allow them to
exchange data and information.
IPC Models
There are two fundamental models of IPC which are as follows −
Shared memory
A region of memory that is shared by cooperating processes is established.
Processes can then exchange information by reading and writing data to the
shared region.
Message passing
Communication takes place by means of messages exchanged between the
cooperating processes. Message passing is useful for exchanging small amounts
of data because no conflicts need be avoided.
It is easier to implement when compared to shared memory by using system calls
and therefore, it requires more time-consuming tasks of the kernel.
For complete python subject tutorials visit : ns lectures youtube channel

Python Global Interpreter Lock (GIL)


A global interpreter lock (GIL) is a mechanism to apply a global lock on an
interpreter. It is used in computer-language interpreters to synchronize and
manage the execution of threads so that only one native thread (scheduled by the
operating system) can execute at a time.

In a scenario where you have multiple threads, what can happen is that both the
thread might try to acquire the memory at the same time, and as a result of which
they would overwrite the data in the memory. Hence, arises a need to have a
mechanism that could help prevent this phenomenon.

Some popular interpreters that have GIL are CPython and Ruby MRI. As most of
you would know that Python is an interpreted language, it has various
distributions like CPython, Jython, IronPython. Out of these, GIL is supported only
in CPython, and it is also the most widely used implementation of Python.
CPython has been developed in both C and Python language primarily to support
and work with applications that have a lot of C language underneath the hood.

Hence, GIL (Source: Understanding the Python GIL):

• Limits the threading operation.


• Parallel execution is restricted.
• The GIL ensures that only one thread runs in the interpreter at once.
• It helps in simplifying various low-level details like memory management.

The impact of the GIL isn’t visible to developers who execute single-threaded
programs.Since the GIL allows only one thread to execute at a time even in a multi-
threaded architecture with more than one CPU core, the GIL has gained a reputation
as an “infamous” feature of Python.

Python Global Interpreter Lock (GIL) is a type of process lock which is used
by python whenever it deals with processes. Generally, Python only uses
only one thread to execute the set of written statements. This means that in
python only one thread will be executed at a time. The performance of the
single-threaded process and the multi-threaded process will be the same in
python and this is because of GIL in python. We can not achieve
multithreading in python because we have global interpreter lock which
restricts the threads and works as a single thread.
For complete python subject tutorials visit : ns lectures youtube channel

Difference Between Lock and RLock


A thread is an entity within a process that can be scheduled for execution.
Also, it is the smallest unit of processing that can be performed in an OS
(Operating System). In simple words, a thread is a sequence of such
instructions within a program that can be executed independently of other
codes.

There are two states of a lock i.e locked and unlocked. A lock is a class in
the threading module whose object generated in the unlocked state and has
two primary methods i.e acquire() and release(). When the acquire() method is
called, it locks the execution of the Lock and blocks it’s execution until the
release() method is called in some other thread which sets it to unlock state.
Locks help us in efficiently accessing a shared resource in a program in order
to prevent corruption of data, it follows mutual exclusion as only one thread
can access a particular resource at a time.
For complete python subject tutorials visit : ns lectures youtube channel

A threading.Lock can only be acquired once, and once acquired it cannot be


acquired again by the same thread or any other thread until it has been released.

A threading.RLock can be acquired more than once by the same thread,


although once acquired by a thread it cannot be acquired by a different thread
until it is been released.

A threading.RLock can be acquired more than once by the same thread,


although once acquired by a thread it cannot be acquired by a different thread
until it is been released.

Importantly, each time the threading.RLock is acquired by the same thread it


must be released the same number of times until it is available to be acquired
again by a different thread. This means that the number of calls to acquire() must
have the same number of calls to release() for the RLock to return to the
unlocked state.

How to Use the Reentrant Lock


Python provides a reentrant lock via the threading.RLock class.

An instance of the RLock can be created and then acquired by threads before
accessing a critical section, and released after the critical section.

For example:

# create a reentrant lock

lock = RLock()

# acquire the lock

lock.acquire()

# ...

# release the lock

lock.release()
For complete python subject tutorials visit : ns lectures youtube channel

Subprocess module
The subprocess module present in Python(both 2.x and 3.x) is used to run new
applications or programs through Python code by creating new processes. It
also helps to obtain the input/output/error pipes as well as the exit codes of
various commands.
To execute different programs using Python two functions of the subprocess
module are used:
1. subprocess.check_call(args, *, stdin=None, stdout=None, stderr=None,
shell=False)
Parameters:
args=The command to be executed. Several commands can be passed as a
String by separated by “;”.
stdin=Value of standard input stream to be passed as (os.pipe()).
stdout=Value of output obtained from standard output stream.
stderr=Value of error obtained(if any) from standard error stream.
shell=Boolean parameter.If True the commands get executed through a new
shell environment.
Return-Value:
The function returns the return code of the command.If the return code is zero,
the function simply returns(command executed successfully) otherwise
CalledProcessError is being raised.
2. subprocess.check_output(args, *, stdin=None, stderr=None, shell=False,
universal_newlines=False)
Parameters:
args=The command to be executed.Several commands can be passed as a
For complete python subject tutorials visit : ns lectures youtube channel

String by separated by “;”.


stdin=Value of standard input stream to be passed as (os.pipe()).
stdout=Value of output obtained from standard output stream.
stderr=Value of error obtained(if any) from standard error stream.
shell=Boolean parameter.If True the commands get executed through a new
shell environment..
universal_newlines=Boolean parameter.If true files containing stdout and stderr
are opened in universal newline mode.
Return Value:
The function returns the return code of the command.If the return code is zero,
the function simply returns the output as a byte string(command executed
successfully) otherwise CalledProcessError is being raised.

c-program:
#include<stdio.h>
int main()
{
printf("Hello my name is nagendrasai, this is c program ");
return 0;
}

Python 3 code to execute the above programs:

import subprocess
import os

def excute():
s = subprocess.check_call("gcc HelloWorld.c -o out1;./out1", shell = True)

if _ _ file _ _ = _ _ main _ _:
execute()

Output:
Hello my name is nagendrasai, this is c program

Note: Although subprocess module is OS independent these commands must


only be executed in Linux environments. Also according to Python
documentation passing shell=True can be a security hazard if combined with
untrusted input.

Multiprocessing module
For complete python subject tutorials visit : ns lectures youtube channel

multiprocessing is a package that supports spawning processes using an API


similar to the threading module. The multiprocessing package offers both local
and remote concurrency, effectively side-stepping the Global Interpreter Lock by
using sub processes instead of threads. Due to this, the multiprocessing module
allows the programmer to fully utilize multiple processors on a given machine. It
runs on both Unix and Windows.
Python Classes/Objects
Python is an object oriented programming language.

Almost everything in Python is an object, with its properties and methods.

A Class is like an object constructor, or a "blueprint" for creating objects.

Create a Class

To create a class, use the keyword class:

Example:

Create a class named MyClass, with a property named x:

Program:

class MyClass:

x=5

print(x)

output:

program2:

class Person:

def fun(self,name,age):

print(" my name is ", name)

print(" my age is ", age)


p1 = Person()

p1.fun("John", 36)

p1.fun("raju", 46)

output:

my name is John
my age is 36
my name is raju
my age is 46

PYTHON CLASS TO REVERSE A STRING:

OUTPUT:

PYTON CLASS TO CALCULATE POWER(X,N)


OUTPUT:
OUTPUT:

You might also like