TYPE THE SUBJECT NAME HERE
SUBJECT CODE
II III
20CSPC301
OBJECT ORIENTED PROGRAMMING
(Common to CSE, IT,CSBS)
UNIT NO V
Lambda Expressions
5.7 Parallel Processing-Reactive
Programming
OBJECT ORIENTED PROGRAMMING (Common to CSE, IT,CSBS)
20CSPC301
Parallel Processing
Definition of Parallel Processing
Parallel processing also called parallel computing this is a type of computation
in which many execution of processes are carried out concurrently.
In computers, parallel processing is the processing of program instructions by
dividing them among multiple processor with the objective of running a
program in less time.
In the earliest computers, only one program ran at a time. According to research
Parallel computing is closely related to concurrent computing — they are
frequently used together, and often conflated, though the two are distinct: it is
possible to have parallelism without concurrency (such as bit-level parallelism),
and concurrency without parallelism (such as multitasking by time-sharing on a
single-core CPU).
OBJECT ORIENTED PROGRAMMING (Common to CSE, IT,CSBS)
20CSPC301
Parallel processing makes a program run faster because there are more engines
(CPUs) running it. In practice, it is often difficult to divide a program in such a
way that separate CPUs can execute different portions without interfering with
each other.
Serial Computing
OBJECT ORIENTED PROGRAMMING (Common to CSE, IT,CSBS)
20CSPC301
Advantages Parallel Processing
•Parallel computing saves time, allowing the execution of applications in a
shorter wall-clock time.
•Solve Larger Problems in a short point of time.
•Compared to serial computing, parallel computing is much better suited for
modeling, simulating and understanding complex, real-world phenomena.
•Many problems are so large that it is impossible to solve them on a single
computer, especially given limited computer memory.
•You can do many things simultaneously by using multiple computing
resources.
•Can using computer resources on the Wide Area Network(WAN) or even on
the internet.
•It can help keep you organized. If you have Internet, then communication and
social networking can be made easier.
•It has massive data storage and quick data computations.
OBJECT ORIENTED PROGRAMMING (Common to CSE, IT,CSBS)
20CSPC301
Disadvantages Parallel Processing
•Programming to target Parallel architecture is a bit difficult but with proper
understanding and practice, you are good to go.
•The use of parallel computing lets you solve computationally and data-intensive
problems using multi-core processors, but, sometimes this effect on some of our
control algorithm and does not give good results and this can also affect the
convergence of the system due to the parallel option.
•The extra cost (i.e. increased execution time) incurred are due to data transfers,
synchronization, communication, thread creation/destruction, etc. These costs can
sometimes be quite large, and may actually exceed the gains due to parallelization.
•Various code tweaking has to be performed for different target architectures for
improved performance.
•Better cooling technologies are required in case of clusters.
•Power consumption is huge by the multi-core architectures
OBJECT ORIENTED PROGRAMMING (Common to CSE, IT,CSBS)
20CSPC301
•Multiprocessing refers to a computer system’s ability to support more than one
process(program) at the same time.
•Multiprocessing operating system enable several programs to run concurrently.
UNIX is one of the most widely used multiprocessing system, but there are many
others, including OS/2 for high-end PCs. Multiprocessing systems are much more
complicated than single-process systems because the operating system must
allocate resource to competing processes in a reasonable manner.
•May also refer to the utilization of multiple CPUs in a single computer system.
•The maximum number of processes you can run at a time is limited by the number
of processors in your computer. If you don’t know how many processors are
present in the machine, the cpu_count() function in multiprocessing will show it.
import multiprocessing as mp
print("Number of processors: ", mp.cpu_count())
OBJECT ORIENTED PROGRAMMING (Common to CSE, IT,CSBS)
20CSPC301
Difference Between Synchronous and Asynchronous execution
•In parallel processing, there are two types of execution: Synchronous and
Asynchronous.
•A synchronous execution is one the processes are completed in the same order in
which it was started. This is achieved by locking the main program until the
respective processes are finished.
•Asynchronous, doesn’t involve locking. As a result, the order of results can get
mixed up but usually gets done quicker.
OBJECT ORIENTED PROGRAMMING (Common to CSE, IT,CSBS)
20CSPC301
There are 2 main objects in multiprocessing to implement parallel execution of a
function: The Pool Class and the Process Class.
1. Pool Class
2. Synchronous execution
Pool.map() and Pool.starmap()
Pool.apply()
1. Asynchronous execution
Pool.map_async() and Pool.starmap_async()
Pool.apply_async())
2. Process Class
OBJECT ORIENTED PROGRAMMING (Common to CSE, IT,CSBS)
20CSPC301
What is Reactive Programming?
•Reactive Programming is a programming language with asynchronous data
stream. Once an event will raise it will react with responsive and non-blocking
manner that’s why it named it as reactive programming.
•In a Reactive Programming context, “Everything is a Stream and acts in a non-
blocking manner when there is data in the stream.”
•This Reactive programming in java is introduced by Netflix organization with
API (Rx-Java)
Why we need Reactive Programming?
•When we have traditional way to develop application why we should go for
reactive programming,
There is four pillar to move towards Reactive Programming
Responsive
Resilient
Elastic
Message Driven
OBJECT ORIENTED PROGRAMMING (Common to CSE, IT,CSBS)
20CSPC301
Responsive:Responsive means if we are raising any events on stream it will return
response in fraction of time as message processing in highly concurrent
environments
Resilient:Resilient means Application should be responsive at the time of failure.
Normally we are integrating multiple modules and each are depend to each other
assume one module is failing so it should not be impact all, it should be propagate.
Elastic:Elastic means our system should be handle N number of request, it should
be well capability to load balance at any condition
Message Driven:Message Driven means asynchronous flow of execution, where
we no need to wait for response after send request to server , once we send request
it move to next business it shouldn’t depends on first one response , the first
request response should be handle by callback mechanism.
OBJECT ORIENTED PROGRAMMING (Common to CSE, IT,CSBS)
20CSPC301
Ideal Use Cases for Implementation of Reactive Programming
•A large number of transaction-processing services, as in the banking sector.
•Notification services of large online shopping applications, like Amazon.
•A share trading business where share prices change simultaneously.
Advantages of Reactive Programming
Improves user experience: The asynchronous nature of RP means that whatever
you program with it will offer a smoother, more responsive product for your users
to interact with.
Improves user experience: The asynchronous nature of RP means that whatever
you program with it will offer a smoother, more responsive product for your users
to interact with.
Simple to compose streams of data: Reactive programming provide the potential
for developers to create, filter, and combine streams of data which can emit a
value, error, and a completed signal, to achieve powerful objectives.
OBJECT ORIENTED PROGRAMMING (Common to CSE, IT,CSBS)
20CSPC301
A lot simpler to do async/threaded work: The method that PR allows you to work
on the data streams causes it becomes less hassle than regular threading.
Avoid “callback hell” or "pyramid of doom": Callback Hell is an anti-pattern
seen in code of asynchronous programming. It is a slang term used to describe and
unwieldy number of nested “if” statements or functions. It usually happens when
there are a lot of callback functions in the code. Developers can avoid the callback
hell with reactive programming because it depends on asynchronous data streams.
High performance in cooperation with Java: RP allows Java apps to have higher
performance with lower memory requirements. This is made possible by avoiding
blocking calls that lead to a process as well as context switches in the OS.
OBJECT ORIENTED PROGRAMMING (Common to CSE, IT,CSBS)
20CSPC301
Disadvantages of Reactive Programming
Hard to learn: RP have a reputation of being difficult since it is entirely different
compared with previous ways of working. This leads to a steep learning curve
when you start using it which may be a shock to some developers.
More memory intensive: Applications will tend to be more memory intensive due
to reactive programming relies on streams over time. This can lead to memory
leakage which could end up seriously slowing things down for users.