0% found this document useful (0 votes)
3 views61 pages

Lecture Notes

Uploaded by

boyaliogluirem
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views61 pages

Lecture Notes

Uploaded by

boyaliogluirem
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PARALLEL PROGRAMMING

Assoc. Prof. Dr. Bora Canbula


Manisa Celal Bayar University
Department of Computer Engineering

[Link] qi9tndw
Week01 Syllabus
Week02 Introduction to Python
Natural Languages vs. Programming Languages
A language is a tool for expressing and recording thoughts.
Computers have their own language called
machine language. Machine languages are created
by humans, no computer is currently capable of
creating a new language. A complete set of known
commands is called an instruction list (IL).

The di erence is that human languages developed


naturally. They are still evolving, new words are
created every day as old words disappear. These
languages are called natural languages.

Elements of a Language
• Alphabet is a set of symbols to build words of
a certain language.
• Lexis is a set of words the language o ers its
users.
• Syntax is a set of rules used to determine if a
certain string of words forms a valid sentence.
• Semantics is a set of rules determining if a
certain phrase makes sense.

Machine Language vs. High-Level Language


The IL is the alphabet of a machine language. It’s the
computer’s mother tongue.

High-level programming language enables humans to write


their programs and computers to execute the programs. It is
much more complex than those o ered by ILs.

A program written in a high-level programming language is


called a source code. Similarly, the le containing the source
code is called the source le.
ff
fi
ff
fi
ff
Week02 Introduction to Python
Compilation vs. Interpretation
There are two di erent ways of transforming a program from
a high-level programming language into machine language:

Compilation: The source code is translated once by getting a


le containing the machine code.

Interpretation: The source code is interpreted every time it is


intended to be executed.

Compilation Interpretation

• The execution of the • You can run the code as soon


translated code is usually as you complete it, there are
faster. no additional phases of
• Only the user has to have the translation.
compiler. The end user may • The code is stored using
use the code without it. programming language, not
• The translated code is stored machine language. You don’t
using machine language. Your compile your code for each
code are likely to remain your di erent architecture.
secret.

• The compilation itself may be • Don’t expect interpretation to


a very time-consuming ramp up your code to high
process speed
• You have to have as many • Both you and the end user
compilers as hardware have the interpreter to run
platforms you want your code your code.
to be run on.
fi
ff
ff
Week02 Introduction to Python
What is Python?
Python is a widely-used, interpreted, object-oriented, and
high-level programming language with dynamic semantics,
used for general-purpose programming.

Python was created by Guido van


Rossum. The name of the Python
programming language comes from
an old BBC television comedy sketch
series called Monty Python’s Flying
Circus.

Python Goals
• an easy and intuitive language just as powerful
as those of the major competitors
• open source, so anyone can contribute
to its development
• code that is as understandable as plain English
• suitable for everyday tasks, allowing for short
development times

Why Python? Why not Python?


• easy to learn • low-level programming
• easy to teach • applications for mobile
• easy to use devices
• easy to understand
• easy to obtain,
install and deploy
Week02 Introduction to Python
Python Implementations
An implementation refers to a program or environment, which
provides support for the execution of programs written in the
Python language.
• CPython is the traditional implementation of Python and it’s
most often called just “Python”.
• Cython is a solution which translate Python code into “C”
to make it run much faster than pure Python.
• Jython is an implementation follows only Python 2, not
Python 3, written in Java.
• PyPy represents a Python environment written in Python-
like language named RPython (Restricted Python), which is
actually a subset of Python.
• MicroPython is an implementation of Python 3 that is
optimized to run on microcontrollers.

Start Coding with Python


• Editor will support you in writing the code. The Python 3
standard installation contains a very simple application
named IDLE (Integrated Development and Learning
Environment).
• Console is a terminal in which you can launch your code.
• Debugger is a tool, which launches your code step-by-step
to allow you to inspect it.

[Link]

s q
[Link] i z
las in-c
fi
Week02 Introduction to Python

Function Name
A function can cause some e ect
or evaluate a value, or both.

Where do functions come from?


• From Python itself
• From modules
• From your code

[Link]

Argument
• Positional arguments
• Keyword arguments
fi
ff
Week02 Introduction to Python
Literals
A literal is data whose values are determined by the literal
itself. Literals are used to encode data and put them into code.

[Link]

•String
•Integer
•Float
•Complex
•Boolean
•Binary
•Octal
•Hexadecimal
•Scienti c Notation
fi
Week02 Introduction to Python
Basic Operators
An operator is a symbol of the programming language, which is
able to operate on the values.

Multiplication Division
Integer Float
Float Float
Float Float
Float Float

Exponentiation Floor Division


Integer Integer
Float Float
Float Float
Float
Float

Modulo Addition
Integer Integer
Float Float
Float
Float
Week02 Introduction to Python
Operator Priorities
An operator is a symbol of the programming language, which is
able to operate on the values.

[Link]
• + (unary)
• - (unary)
• ** (right-sided binding)
• *
• /
• //
• % (left-sided binding)
• + (binary)
• - (binary)
Week02 Introduction to Python
Variables
Variables are symbols for memory addresses.

[Link]
Week02 Introduction to Python
Identi er Names
For variables, functions, classes etc. we use identi er names.
We must obey some rules and we should follow some naming
conventions.
• Names are case sensitive.
• Names can be a combination of letters, digits, and
underscore.
• Names can only start with a letter or underscore,
can not start with a digit.
• Keywords can not be used as a name.

s q u i z
s
[Link]
-cla in
fi
fi
Week02 Introduction to Python
Your First Homework

Week01/info_ rstname_lastname.py
A string variable with the name student_id that contains
your student id.
A string variable with the name full_name that contains
your full name.

Week02/types_ rstname_lastname.py
An integer with the name: my_int
A oat with the name: my_ oat
A boolean with the name: my_bool
A complex with the name: my_complex
fl
fi
fi
fl
Week02 Introduction to Python
Your First Homework
Week03 Introduction to Python
Equality & Identity & Comparison

Equality

Left- or Right-sided?

Inequality Updated Priority Table


Operator Type

+, - unary

** binary
Comparison Chaining *, /, //, % binary

+, - binary

<, <=, >, >= binary

!=, == binary

Using one of the comparison operators in Python, write


QUESTION

a simple two-line program that takes the parameter n as


input, which is an integer, prints False if n is less than
100, and True if n is greater than or equal to 100.

Conditional Execution
if statement

Ternary Operator
Week03 Introduction to Python
Loops
• The program generates a random
number between 1 and 10.
• The user is asked to guess the
QUESTION

number.
• The user is given feedback if the
guess is too low or too high.
• The user is asked to guess again
until the correct number is guessed.

• The user is asked to enter a


QUESTION

number.
• The program prints the numbers
from 0 to n-1.

break and continue

Can we use while/for with else?


Week03 Introduction to Python
Week03/[Link]

s
[Link]
s q u iz
cla in-

Week03/pyramid_ rst_last.py Week03/sequences_ rst_last.py


fi
fi
Week04 Introduction to Python
Exception Handling
Exception handling is a mechanism in Python to handle
runtime errors gracefully without crashing the program.

Handling an Exception Handling Multiple Exceptions

Handling All Exceptions

Using else Block

Using nally Block

Raising Exceptions

Can we create custom exceptions?


fi
Week04 Introduction to Python
Identi er Names
For variables, functions, classes etc. we use identi er names.
We must obey some rules and we should follow some naming
conventions.

Naming Conventions from PEP 8


• Names to Avoid
Never use the characters ‘l’ (lowercase letter el), ‘O’
(uppercase letter oh), or ‘I’ (uppercase letter eye) as single
character variable names.
• Packages
Short, all-lowercase names without underscores
• Modules
Short, all-lowercase names, can have underscores
• Classes
CapWords (upper camel case) convention
• Functions
snake_case convention
• Variables
snake_case convention
• Constants
ALL_UPPERCASE, words separated by underscores

Underscore Usage in Identi er Names


• _single_leading_underscore
Weak “internal use” indicator.
from M import * does not import objects whose names start
with an underscore.
• single_trailing_underscore_
Used by convention to avoid con icts with keyword.
• __double_leading_underscore
When naming a class attribute, invokes name mangling
(inside class FooBar, __boo becomes _FooBar__boo)
• __double_leading_and_trailing_underscore__
“magic” objects or attributes that live in user-controlled
namespaces (__init__, __import__, etc.). Never invent such
names; only use them as documented.
fi
fi
fl
fi
Week04 Introduction to Python
Functions
Functions are de ned by using def keyword, name,
and the parenthesized list of formats parameters.
Naming Convention from PEP8
Function names should be lowercase, with words separated by
underscores as necessary to improve the readability.

Basic Function De nition Input and Output Arguments

Default Values for Arguments

Type Hints and Default Values for Arguments

P 3 10 7
PE
Multiple Type Hints for Arguments ( > Python 3.10 )

o n 3.10
>P y t h
Lambda Functions

Function Docstrings

7
PEP 25
fi
fi
Week04 Introduction to Python
Docstrings PEP 257
A docstring is a string literal that occurs as the rst statement in
a module, function, class or method de nition. Such a docstring
becomes the __doc__ special attribute of that object.
One-line Docstrings

Multi-line Docstrings

Docutils and Sphinx are tools to automatically create documentations

reST (reStructuredText) Google

Some other formats are Epytext


(javadoc), Numpydoc, etc.
fi
fi
Week04 Introduction to Python
Parameter Kinds PEP 362
Kind describes how argument values are bound to the parameter.
The kind can be xed in the signature of the function.

Positional-or-Keyword (Standard Binding)

Positional-or-Keyword and Keyword-Only

Positional-Only and Positional-or-Keyword and Keyword-Only

E P 45 7
P
*args and **kwargs
fi
Week04 Introduction to Python
Function Attributes PEP 232
Functions already have a number of attributes such as __doc__,
__annotations__, __defaults__, etc. Like everything in Python,
functions are also objects, therefore, user can add a dictionary as
attributes by using get / set methods to __dict__.
Week04 Introduction to Python
Function Attributes PEP 232
Functions already have a number of attributes such as __doc__,
__annotations__, __defaults__, etc. Like everything in Python,
functions are also objects, therefore, user can add a dictionary as
attributes by using get / set methods to __dict__.
Week04 Introduction to Python
Nested Scopes PEP 227
Function objects can have methods. These methods can be used
as inner functions and can be useful for encapsulation.

Getter and Setter Methods


Week04 Introduction to Python
Decorators
Decorators take a function as argument and returns a function. They
are used to extend the behavior of the wrapper function, without
modifying it. So they are very useful for dealing with code legacy.
Traditional Way Pythonic Way

Decorators with Arguments Decorator Chain

s q ui z
-cla s
[Link]
in
Week04 Introduction to Python
Week04/functions_ rstname_lastname.py

custom_power custom_equation
A lambda function A function returns oat
Two parameters (x and e) Five integer parameters (x, y, a, b, c)
x is positional-only x is positional-only with default value 0
e is positional-or-keyword y is positional-only with default value 0
x has the default value 0 a is positional-or-keyword with default value 1
e has the default value 1 b is positional-or-keyword with default value 1
Returns x**e c is keyword-only with default value 1
Function signature must include all annotations
Docstring must be in reST format.
Returns (x**a + y**b) / c

fn_w_counter Examples
A function returns a tuple of an int
and a dictionary
Function must count the number of
calls with caller information
Returning integer is the total number
of calls
Returning dictionary with string keys
and integer values includes the caller
( _ _name_ _ ) as key, the number of
call coming from this caller as value.

Week04/decorators_ rstname_lastname.py
performance
A decorator which measures the performance of functions and also saves
some statistics.
Has three attributes: counter, total_time, total_mem
Attribute counter stores the number of times that the decorator has been
called.
Attribute total_time stores the number of total time that the functions
took.
Attribute total_mem stores the total memory in bytes that the functions
consumed.
fl
fi
fi
Week05 Asynchronous Programming with Python
The problems in computer programming can be categorized
based on the primary source of their performance
bottlenecks.
I/O-bound Problems
While solving an I/O-bound problem, the system spends a
signi cant amount of time waiting for input/output
operations.
Subcategories can be Disk I/O (reading or writing to a hard
drive) and Network I/O (waiting for data from a remote
server).
The solutions often involve asynchronous programming,
caching, or optimizing the I/O operations.

CPU-bound Problems
For CPU-bound problems, computational processing is the
bottleneck.
Speeding up the computation requires either a faster CPU
or optimizing the computation itself.
Parallel processing, algorithm optimization, or o oading
computations to other systems or specialized hardware (like
GPUs) are common strategies to overcome these problems.

Memory-bound Problems
Problems where the primary constraint is the system’s
memory.
Solutions can involve optimizing data structures, utilizing
external memory storage, or employing algorithms that are
more memory-e cient.
fi
ffi
ffl
Week05 Asynchronous Programming with Python

Coroutines are a generalization of subroutines (functions) used for


Do you know
cooperative multitasking. any Callable
Unlike functions that return once and do
state, coroutines
not maintainwhich can pause can pause their execution and later
its execution?
continue from where they left o .

return
Regular functions returning a speci ed value back to the
caller and terminates the function’s execution.
Once the function returns a value using return, its state
lost. Subsequent calls to the function start the execution
from the beginning of the function.
Used to compute a value and return it to caller immediately.

yield
Used in special functions known as generators. Produces a
series of values for iteration using a lazy evaluation
approach (values are generated on-the- y, not stored in
memory).
When a function using the yield keyword is called, it returns
a generator object without even beginning execution of the
function.
Upon calling next(), the function runs until it encounters the
yield keyword. The function’s execution is paused, and the
yielded value is returned. Subsequent calls to next() resume
the function’s execution immediately after the last yield
statement.
Once all values have been yielded, the generator raises a
StopIteration exception.

Can you write your own generator by using


some magic methods in a class?
ff
fi
fl
Week05 Asynchronous Programming with Python
Coroutines are a generalization of subroutines (functions) used for
cooperative multitasking. Unlike functions that return once and do
not maintain state, coroutines can pause their execution and later
continue from where they left o . In
c
Ef reas
Traditional synchronous programming runs line by line. cie ing
In I/O-bound problems, the program waits for the operation. nc
Asynchronous programming allows tasks to run concurrently. y

Can you increase the ef ciency in real world


problems with this technique?
[Link]
[Link]
How to implement Coroutines in Python?
Coroutines declared with the async/await syntax is the best
practice of writing asynchronous applications in Python.
✓ Handle many tasks concurrently without multi-threading.
✓ Improve performance for I/O-bound tasks.

Sync F1 [5 sec] F2 [2 sec]

Async C1 [5 sec] GAIN


C2 [2 sec]

Can you write your own awaitable by using


some magic methods in a class?
fi
fi
ff
Week05 Asynchronous Programming with Python

s q
[Link]
a s uiz
cl in-

Week05/awaitme_ rstname_lastname.py

awaitme
A decorator which turns any function into a coroutine.
It must pass all the arguments to the function properly.
If function returns any value, so the decorator returns it.

Rules for your pull requests


✓ Please run your code rst in your computer, do not
submit codes with syntax errors.
✓ Submit your code to WeekXX folder. WeekXX/hw is
for me to move accepted works.
✓ If a change is requested, please edit the existing pull
request, don’t open a new one.

PROJECT: Implement the cookie problem in Python


✓ Application optimizes any recipe.
✓ You can group up to 4 students.
✓ Backend with Python.
✓ Tests with Pytest.
✓ Frontend with HTML + CSS + JS.
✓ 5 minutes presentation in English.
fi
fi
Week06 Asynchronous Programming with Python

Wrap-up: Use what we have learned so far in a project!

Using requests module


Week06 Asynchronous Programming with Python

Wrap-up: Use what we have learned so far in a project!

Using logging module


Week06 Asynchronous Programming with Python

Wrap-up: Use what we have learned so far in a project!

Solving the problems of requests module

Context Manager

[Link]
Week06 Asynchronous Programming with Python

Wrap-up: Use what we have learned so far in a project!

Time to go asynchronous!

Asynchronous
Context Manager

[Link]
Week06 Asynchronous Programming with Python

s q
[Link]
a s uiz
cl in-

Week06/timer_ rstname_lastname.py
Timer
Create a class Timer that measures the time taken
by the block of code it manages.
Timer class must be a context manager.
The class must have two public attributes start_time
and end_time, which are for the starting and the ending
times, respectively.

Rules for your pull requests


✓ Please run your code rst in your computer, do not
submit codes with syntax errors.
✓ Submit your code to WeekXX folder. WeekXX/hw is
for me to move accepted works.
✓ If a change is requested, please edit the existing pull
request, don’t open a new one.

Good Luck
for your midterm!
fi
fi
Week07 Multithread Programming with Python

Monte Carlo Estimation of Pi


y
(area of square) = (side)2
1 (area of circle) = (constant) × (radius)2
................
............. (side) = 2 (radius) = 1

................. 1
. .. x
(area of circle) (constant)
=
(area of square) 4

(inner points) (constant)


=
(total points) 4

(inner points)
estimated value of π = 4 ×
(total points)

increase the number of points to increase the precision


Week07 Multithread Programming with Python

Creating Threads
From a Function From a Class

with Arguments

Synchronization

Daemon Threads
Week07 Multithread Programming with Python

Revisit the pi estimation problem with threads


y

.1...............
.............
................. 1
. .. x

s q u i z
s
[Link]
-cla in
Week07/threaded_ rstname_lastname.py
threaded
Create a decorator that creates n number of threads
from a function
Decorator must accept an integer argument: n
The decorator must create, start and then nally
synchronize the threads by waiting them to nish

Rules for your pull requests


✓ Please run your code rst in your computer, do not
submit codes with syntax errors.
✓ Submit your code to WeekXX folder. WeekXX/hw is
for me to move accepted works.
✓ If a change is requested, please edit the existing pull
request, don’t open a new one.
fi
fi
fi
fi
Week08 Multithread Programming with Python

Monte Carlo Estimation of Pi


y
(area of square) = (side)2
1 (area of circle) = (constant) × (radius)2
................
............. (side) = 2 (radius) = 1

................. 1
. .. x
(area of circle) (constant)
=
(area of square) 4

(inner points) (constant)


=
(total points) 4

(inner points)
estimated value of π = 4 ×
(total points)

Embarrassingly Parallel
A problem type, which its solution requires very little or
even no e ort to parallelize. The key point is that there
is no need of communication between the tasks.
Atomic Part
ff
Week08 Multithread Programming with Python

Atomic Part
Convert Atomic Operation to Thread

Create another thread to generate many threads from atomic


operation class and coordinate to complete the solution
Week08 Multithread Programming with Python

Condition to nalize
the solution
The maximum
number of
threads to run
simultaneously
Creator Thread for Atomic Operations

The minimum unit job


for each atomic operation
fi
Week08 Multithread Programming with Python

Create a Main Thread

Only one thread is running a time

GIL: Global Interpreter Lock


It is a mutex that protects access to Python objects, preventing
multiple threads from executing Python bytecode at once. This
lock can be a signi cant limitation for CPU-bound problems but
it is necessary mainly because CPython’s memory management
is not thread-safe.
fi
Week08
Numba: Just-In-Time (JIT) Compiler for Python Multithread Programming with Python

Numba: Just-In-Time Compiler

The method must be static


to operate independently
of instance-speci c data.
It should be like
a standalone function.

With these options,


Numba compiles the function,
therefore, it runs entirely
without the Python interpreter.

Now the threads can run together


fi
Week08 Multithread Programming with Python

Test with Threads


Simple Counter
Increase the Counter

Unpredictable Results

What’s under the hood


when we change the Increasing a value sequentially
value of a variable count = 0 count = 1 count = 2 count = 3

count = 1 count = 2 count = 3 count = 4

Time
Compiler Explorer Never overlaps with each other
[Link]
Week08 Multithread Programming with Python

With Multiple Threads

Time
count = 0

Lock
Without a Mutex
Time

count = 0

Race Condition count = 1


count = 1

Lock
count = 1
count = 1

count = 2
count = 1
count = 2

Lock
count = 2

count = 2
count = 3
count = 2
Using Locks

count = 2

count = 3

count = 3
count = 3

count = 3

count = 3 Now
it is
count = 4

count = 4 safe
count = 4
count = 4
but
very
count = 5
slow!
9 increments s s q u iz
[Link]

but count is 5 in -cla


r/dV7XcgaJTX
Week09 Thread Synchronization
Synchronization Concepts
Synchronization ensures that threads coordinate their
actions e ectively when accessing shared resources.
Without proper synchronization, issues like race
conditions, deadlock, and inconsistent data states
can arise, leading to unpredictable program behavior.

Deadlock Deadlock
When two or more
threads are waiting on
each other to release
locks, causing an
in nite waiting state.
Semaphore
A semaphore is used to
limit the number of
threads accessing a
shared resource. A
counter that decreases
when a thread acquires
it and increases when a
thread releases it.
Barrier
It ensures that multiple
threads reach a certain
point in execution
before any of them
proceed.
Condition
It is used for
synchronization by
communicating
between threads
fi
ff
Week09 Thread Synchronization
Synchronization Concepts
Synchronization ensures that threads coordinate their
actions e ectively when accessing shared resources.
Without proper synchronization, issues like race
conditions, deadlock, and inconsistent data states
can arise, leading to unpredictable program behavior.

Deadlock Semaphore
When two or more
threads are waiting on
each other to release
locks, causing an
in nite waiting state.
Semaphore
A semaphore is used to
limit the number of
threads accessing a
shared resource. A
counter that decreases
when a thread acquires
it and increases when a
thread releases it.
Barrier
It ensures that multiple
threads reach a certain
point in execution
before any of them
proceed.
Condition
It is used for
synchronization by
communicating
between threads
fi
ff
Week09 Thread Synchronization
Synchronization Concepts
Synchronization ensures that threads coordinate their
actions e ectively when accessing shared resources.
Without proper synchronization, issues like race
conditions, deadlock, and inconsistent data states
can arise, leading to unpredictable program behavior.

Deadlock Barrier
When two or more
threads are waiting on
each other to release
locks, causing an
in nite waiting state.
Semaphore
A semaphore is used to
limit the number of
threads accessing a
shared resource. A
counter that decreases
when a thread acquires
it and increases when a
thread releases it.
Barrier
It ensures that multiple
threads reach a certain
point in execution
before any of them
proceed.
Condition
It is used for
synchronization by
communicating
between threads
fi
ff
Week09 Thread Synchronization
Synchronization Concepts
Synchronization ensures that threads coordinate their
actions e ectively when accessing shared resources.
Without proper synchronization, issues like race
conditions, deadlock, and inconsistent data states
can arise, leading to unpredictable program behavior.

Deadlock Condition
When two or more
threads are waiting on
each other to release
locks, causing an
in nite waiting state.
Semaphore
A semaphore is used to
limit the number of
threads accessing a
shared resource. A
counter that decreases
when a thread acquires
it and increases when a
thread releases it.
Barrier
It ensures that multiple
threads reach a certain
point in execution
before any of them
proceed.
Condition
It is used for
synchronization by
communicating
between threads
fi
ff
Week09 Thread Synchronization

• Five philosophers, sitting around a circular table.


• There are ve chairs and ve plates full of spaghetti.
• Between each pair of plates, there is a single fork.
• A philosopher can only think or eat.
• To eat, a philosopher must have two forks: one from their left
and one from their right.
• After eating, they put down both forks, and then they start
thinking again.

The problem is to design a protocol that allows them eating

Why is it challenging? What can happen?

Implementing the Problem Deadlock


All philosophers hold one
The modules:
• threading, random, time fork and wait inde nitely for
the other one.
The classes:
• Philosopher (Thread)
• Fork (Custom Lock) Starvation
The inputs: The solution of deadlock
• n (number of philosophers) results in some
• spaghetti (amount in integers) philosophers never eat.
fi
fi
fi
Week09 Thread Synchronization

• Five philosophers, sitting around a circular table.


• There are ve chairs and ve plates full of spaghetti.
• Between each pair of plates, there is a single fork.
• A philosopher can only think or eat.
• To eat, a philosopher must have two forks: one from their left
and one from their right.
• After eating, they put down both forks, and then they start
thinking again.

The problem is to design a protocol that allows them eating

Why is it challenging? What can happen?

Implementing the Problem

The modules: Odd-Even Strategy


• threading, random, time Philosophers are numbered from 1 to
n. Philosophers with odd numbers pick
The classes:
up their left fork rst and then their
• Philosopher (Thread) right fork. Philosophers with even
• Fork (Custom Lock) numbers do the opposite.
The inputs:
• n (number of philosophers) s q u i z
[Link]
• spaghetti (amount in integers) in -clas
r/xE9vDmAMqR
fi
fi
fi
Week10 Multiprocess Programming with Python

From Function
From Class
Pass Arguments Creating Processes

Fork Spawn Forkserver


‣ Only on UNIX ✓ Cross-platform ‣ Only on UNIX
✓ Fast startup - Slow startup ‣ Moderate startup
‣ Moderate safety ✓ High safety ✓ High safety
Week10 Multiprocess Programming with Python

Share Data Between Processes


Pipes are the fundamental IPC (inter-process communication)
mechanism provided by the operating system. They are used for
one-way (simplex) or two-way (duplex) communication between
processes.
Using a one-way pipe
Week10 Multiprocess Programming with Python

Share Data Between Processes


Pipes are the fundamental IPC (inter-process communication)
mechanism provided by the operating system. They are used for
one-way (simplex) or two-way (duplex) communication between
processes.
Week10 Multiprocess Programming with Python

Share Data Between Processes


Pipes are the fundamental IPC (inter-process communication)
mechanism provided by the operating system. They are used for
one-way (simplex) or two-way (duplex) communication between
processes.
Using a one-way pipe
Week10 Multiprocess Programming with Python

Share Data Between Processes


Queues are built on top of pipes and used to facilitate easier and
thread-safe communication between processes. They are useful for
sending messages or tasks between multiple producer and
consumer processes, the result queue is used to collect the results.
Week10 Multiprocess Programming with Python

Share Data Between Processes


Queues are built on top of pipes and used to facilitate easier and
thread-safe communication between processes. They are useful for
sending messages or tasks between multiple producer and
consumer processes, the result queue is used to collect the results.
Using a queue to collect the results from processes
Week10 Multiprocess Programming with Python

Share Data Between Processes


Value and Array are used to share simple data types or arrays among
multiple processes in a way that ensures thread-safe and process-
safe operations. They support “ctypes” primitives (int, double, bool).
Week10 Multiprocess Programming with Python

Share Data Between Processes


Value and Array are used to share simple data types or arrays among
multiple processes in a way that ensures thread-safe and process-
safe operations. They support “ctypes” primitives (int, double, bool).
Using a queue to collect the results from processes
Week10 Multiprocess Programming with Python

Best Practice for Multiprocessing


Creating a pool as a collection of worker processes that execute
tasks in parallel. They run independently and concurrently.
Using [Link] blocks the main process until the result is read. So
the processing of the function is distributed across multiple
processes, and the results are returned in a list.
Using a pool to of oad the tasks to worker processes

q u i z
[Link]
s
🙋 Good luck, see you again in -clas
r/YmkRr7SKE4
fl

You might also like