SlideShare a Scribd company logo
z
Concurrency
Models in Python
And how to choose between them
Isac Casapu
isac@pricesearcher.com
z
Why concurrency?
 Multiple, distinct activities that are time consuming
 Processing intensive (CPU bound)
 Waiting on I/O (storage, network)
z
Concurrency Strategies
 Multiple Processes
 Share nothing by default
 Multiple Threads
 Potentially run on multiple CPUs
 All memory and context by default shared
 Asynchronous, Cooperative Multitasking
 Submit time-consuming requests and get called when done
 Typically runs in a single OS thread
z
Async vs Multi-Threading
ExecutionTime
Source: Python Async - Leonardo Rossetti
z
What Async code looks like
 Explicitly indicate functions called asynchronously
 Explicitly relinquish control
 Change any code that blocks to Async equivalent
 E.g. aiohttp, aiobotocore, aiomysql…
async def crawl(self):
await self.request_queue.load_robots_file(self.base_url)
self.robots_file = self.request_queue.robots_file
self.request_queue.enqueue(self.base_url)
await self.request_queue.run()
return
Excerpt from a PoC crawler written using AioHTTP.
z
Multithreading in Python
 Python interpreter is single-threaded
 1 thread at a time can execute pure
Python code
 Native code can work concurrently
 CPU bound: Numeric, XML parsing
 Making system calls
 Anything else in C, Fortran etc.
 Alternative VMs are thread safe:
 .Net (IronPython), JVM (Jython)
z
Recommendations
Your
Workload
I/O
Bound
?
Mostly
Native
?
No
Multithreading
Library
Use?
Yes
Yes
AsyncIO
No
Yes
Multiprocessing
No
z
More Info
Presentations going into greater depth:
 Introduction to Python Asyncio
 Enhance Your Python Code Beyond GIL
Code examples for asynchronous programming:
 A simple web crawler using aiohttp
 Capturing streaming output from a subprocess using AsyncIO
Isac Casapu
isac@pricesearcher.com

More Related Content

PDF
How to make the Fastest C# Serializer, In the case of ZeroFormatter
PDF
Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017
KEY
Amepad lt(tmpfs)
PDF
MongoDB, Node.js And You: PART II
PDF
[COSCUP 2018] uTensor C++ Code Generator
ODP
Linux multiplexing
PDF
Logging kernel oops and panic
PPTX
epoll() - The I/O Hero
How to make the Fastest C# Serializer, In the case of ZeroFormatter
Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017
Amepad lt(tmpfs)
MongoDB, Node.js And You: PART II
[COSCUP 2018] uTensor C++ Code Generator
Linux multiplexing
Logging kernel oops and panic
epoll() - The I/O Hero

What's hot (20)

PDF
Rust All Hands Winter 2011
PDF
Intro to Rust 2019
PDF
Glusterfs session #18 intro to fuse and its trade offs
PDF
Gluster dev session #3 xlator interface
PDF
Many Cores Java - Session One: Threads and Threads
PDF
Threads and Threads
PDF
Glusterfs session #10 locks xlator inodelks
PDF
Open ZFS Keynote (public)
PDF
Intro to introducing rust to ruby
PPTX
Rusty Python
PDF
Tarantool 1.6 talk at SECR 2014 conference
PDF
Tips on High Performance Server Programming
ZIP
Redis and Ohm
PDF
Efficient Bytecode Analysis: Linespeed Shellcode Detection
PDF
The Rust Programming Language
PPTX
Practical Glusto Example
PDF
What makes a LDAP server running fast ? An bit of insight about the various b...
PDF
A Look at Command Line Swift
PPT
Multithreading Presentation
PDF
Chainer Development Plan 2015/12
Rust All Hands Winter 2011
Intro to Rust 2019
Glusterfs session #18 intro to fuse and its trade offs
Gluster dev session #3 xlator interface
Many Cores Java - Session One: Threads and Threads
Threads and Threads
Glusterfs session #10 locks xlator inodelks
Open ZFS Keynote (public)
Intro to introducing rust to ruby
Rusty Python
Tarantool 1.6 talk at SECR 2014 conference
Tips on High Performance Server Programming
Redis and Ohm
Efficient Bytecode Analysis: Linespeed Shellcode Detection
The Rust Programming Language
Practical Glusto Example
What makes a LDAP server running fast ? An bit of insight about the various b...
A Look at Command Line Swift
Multithreading Presentation
Chainer Development Plan 2015/12
Ad

Similar to Concurrency models in python (20)

PPTX
Async programming and python
PDF
.Net Multithreading and Parallelization
PPT
Intro To .Net Threads
PPT
Operating System 4
PPT
Operating System 4 1193308760782240 2
PDF
CH04.pdf
PPTX
Asynchronous Programming in .NET
PPTX
Transactional Memory
PPT
AsyncIO To Speed Up Your Crawler
PDF
Node.js introduction
PPTX
Cgroups, namespaces and beyond: what are containers made from?
PDF
Php and threads ZTS
PDF
Here comes the Loom - Ya!vaConf.pdf
ODP
Asynchronous I/O in NodeJS - new standard or challenges?
PPTX
Task parallel library presentation
PDF
HOW TO DEAL WITH BLOCKING CODE WITHIN ASYNCIO EVENT LOOP
PPTX
Async Programming in C# 5
PPTX
Asynchronous programming in ASP.NET
PPTX
Python, async web frameworks, and MongoDB
PPTX
Everything you wanted to know about writing async, concurrent http apps in java
Async programming and python
.Net Multithreading and Parallelization
Intro To .Net Threads
Operating System 4
Operating System 4 1193308760782240 2
CH04.pdf
Asynchronous Programming in .NET
Transactional Memory
AsyncIO To Speed Up Your Crawler
Node.js introduction
Cgroups, namespaces and beyond: what are containers made from?
Php and threads ZTS
Here comes the Loom - Ya!vaConf.pdf
Asynchronous I/O in NodeJS - new standard or challenges?
Task parallel library presentation
HOW TO DEAL WITH BLOCKING CODE WITHIN ASYNCIO EVENT LOOP
Async Programming in C# 5
Asynchronous programming in ASP.NET
Python, async web frameworks, and MongoDB
Everything you wanted to know about writing async, concurrent http apps in java
Ad

Recently uploaded (20)

PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
System and Network Administration Chapter 2
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
top salesforce developer skills in 2025.pdf
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
How Creative Agencies Leverage Project Management Software.pdf
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Nekopoi APK 2025 free lastest update
PDF
Understanding Forklifts - TECH EHS Solution
Navsoft: AI-Powered Business Solutions & Custom Software Development
VVF-Customer-Presentation2025-Ver1.9.pptx
CHAPTER 2 - PM Management and IT Context
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Adobe Illustrator 28.6 Crack My Vision of Vector Design
System and Network Administration Chapter 2
Odoo POS Development Services by CandidRoot Solutions
Design an Analysis of Algorithms I-SECS-1021-03
PTS Company Brochure 2025 (1).pdf.......
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
How to Choose the Right IT Partner for Your Business in Malaysia
top salesforce developer skills in 2025.pdf
wealthsignaloriginal-com-DS-text-... (1).pdf
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
How Creative Agencies Leverage Project Management Software.pdf
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Nekopoi APK 2025 free lastest update
Understanding Forklifts - TECH EHS Solution

Concurrency models in python

  • 1. z Concurrency Models in Python And how to choose between them Isac Casapu isac@pricesearcher.com
  • 2. z Why concurrency?  Multiple, distinct activities that are time consuming  Processing intensive (CPU bound)  Waiting on I/O (storage, network)
  • 3. z Concurrency Strategies  Multiple Processes  Share nothing by default  Multiple Threads  Potentially run on multiple CPUs  All memory and context by default shared  Asynchronous, Cooperative Multitasking  Submit time-consuming requests and get called when done  Typically runs in a single OS thread
  • 4. z Async vs Multi-Threading ExecutionTime Source: Python Async - Leonardo Rossetti
  • 5. z What Async code looks like  Explicitly indicate functions called asynchronously  Explicitly relinquish control  Change any code that blocks to Async equivalent  E.g. aiohttp, aiobotocore, aiomysql… async def crawl(self): await self.request_queue.load_robots_file(self.base_url) self.robots_file = self.request_queue.robots_file self.request_queue.enqueue(self.base_url) await self.request_queue.run() return Excerpt from a PoC crawler written using AioHTTP.
  • 6. z Multithreading in Python  Python interpreter is single-threaded  1 thread at a time can execute pure Python code  Native code can work concurrently  CPU bound: Numeric, XML parsing  Making system calls  Anything else in C, Fortran etc.  Alternative VMs are thread safe:  .Net (IronPython), JVM (Jython)
  • 8. z More Info Presentations going into greater depth:  Introduction to Python Asyncio  Enhance Your Python Code Beyond GIL Code examples for asynchronous programming:  A simple web crawler using aiohttp  Capturing streaming output from a subprocess using AsyncIO Isac Casapu isac@pricesearcher.com