Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Greenlet-based concurrency
Search
Goran Peretin
July 03, 2013
Programming
690
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Greenlet-based concurrency
Slides from EuroPython 2013 talk Greenlet-based concurrency.
Goran Peretin
July 03, 2013
More Decks by Goran Peretin
See All by Goran Peretin
Webcamp Zagreb 2013
gperetin
1
340
On Concurrency
gperetin
1
420
WebcampZG 2012
gperetin
1
450
Other Decks in Programming
See All in Programming
すこし踏み込む CancellationToken
htkym
2
1.7k
更なる可用性を求めて、5年間運用したKotlinのアプリケーションをGoでリプレイスする話
ken_tunc
0
580
Atomic Design, Enforced: Scaling Mobile Design Systems (next.app devCon / droidCon Berlin 2026)
steliosf
PRO
0
160
TechTown - Creating Composable Callables in Contemporary C++
rollbear
0
100
Jetpack Compose Mechanisms
skydoves
2
720
コードで差別化できない時代の ソフトウェアエンジニアリング / Software Engineering in an Era Where Code Doesn’t Differentiate
kishida
2
160
Apple Intelligence を用いた個人情報誤送信防止、及びユーザーリクエスト体験の改善について
yukiny
0
330
RAG の “R” を Swift で覗いてみる 〜「意味から探す」検索の仕組み〜
nao_randd
0
150
コードレビューのボトルネックを"する側"と"される側"の両面から解消する
yub0n
2
1.6k
SalesForceを内製化!? ~ HR事業を支える顧客管理基盤のインフラを大公開 ~
oku053
0
170
SREの越境 / SRE Collaboration
y0hgi
2
300
CodeRabbitの効果検証と過ごしてみた3ヶ月
armondando
0
230
Featured
See All Featured
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
For a Future-Friendly Web
brad_frost
184
10k
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
74
43k
Amusing Abliteration
ianozsvald
1
340
Leo the Paperboy
mayatellez
10
2.4k
The Curious Case for Waylosing
cassininazir
1
570
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
300
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
2
1.8k
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
1
280
Heart Work Chapter 1 - Part 1
lfama
PRO
10
37k
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
1
570
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
Transcript
Greenlet-based concurrency Goran Peretin @gperetin
Who am I? ✤ Freelancer ✤ Interested in concurrent, parallel
and distributed systems
What is this about? ✤ understand what <buzzword> is ✤
when should you use <buzzword> ✤ concurrency as execution model (as opposed to composition model)
There will be no... ✤ Turnkey solutions ✤ GIL ✤
Details
Buzzwords ahead!
✤ concurrent vs parallel execution ✤ cooperative vs preemptive multitasking
✤ CPU bound vs IO bound task ✤ thread-based vs event-based concurrency
Mandatory definitions
Parallel execution ✤ Simultaneous execution of multiple tasks ✤ Must
have multiple CPUs
Concurrent execution ✤ Executing multiple tasks in the same time
frame ✤ ... but not necessarily at the same time ✤ Doesn’t require multiple CPU cores
Why do we want concurrent execution? ✤ We need it
- more tasks than CPUs ✤ CPU is much faster than anything else
Thread-based concurrecy ✤ Executing multiple threads in the same time
frame ✤ OS scheduler decides which thread runs when
How OS scheduler switches tasks? ✤ When current thread does
IO operation ✤ When current thread used up it’s time slice
How OS scheduler switches tasks? ✤ When current thread does
IO operation ✤ When current thread used up it’s time slice Preemptive multitasking
None
Mandatory GIL slide ✤ Global Interpreter Lock ✤ One Python
interpreter can run just one thread at any point in time ✤ Only problem for CPU bound tasks
CPU bound vs IO bound ✤ CPU bound - time
to complete a task is determined by CPU speed ✤ calculating Fibonacci sequence, video processing... ✤ IO bound - does a lot of IO, eg. reading from disk, network requests... ✤ URL crawler, most web applications...
Python anyone? ✤ import threading ✤ Python threads - real
OS threads
Houston, we have a...
Problem? ✤ Lots of threads ✤ Thousands
Benchmarks!
Sample programs ✤ Prog 1: spawn some number of threads
- each sleeps 200ms ✤ Prog 2: spawn some number of threads - each sleeps 90s
Prog 1 ✤ Sleep 200ms # of threads 100 1K
10K 100K Time 207 ms 327 ms 2.55 s 25.42 s
Prog 2 ✤ Sleep 90s # of threads 100 1K
10K 100K RAM ~4.9 GB ~11.8 GB ~82GB ? (256GB)
... and more ✤ Number of threads is limited ✤
Preemptive multitasking
We need ✤ Fast to create ✤ Low memory footprint
✤ We decide when to switch
Green threads!
Green threads ✤ Not managed by OS ✤ 1:N with
OS threads ✤ User threads, light-weight processes
Greenlets ✤ “...more primitive notion of micro- thread with no
implicit scheduling; coroutines, in other words.” ✤ C extension
Greenlets ✤ Micro-thread ✤ No implicit scheduling ✤ Coroutines
Coroutine ✤ Function that can suspend it’s execution and then
later resume ✤ Can also be implemented in pure Python (PEP 342) ✤ Coroutines decide when they want to switch
Coroutine ✤ Function that can suspend it’s execution and then
later resume ✤ Can also be implemented in pure Python (PEP 342) ✤ Coroutines decide when they want to switch Cooperative multitasking
Cooperative multitasking ✤ Each task decides when to give others
a chance to run ✤ Ideal for I/O bound tasks ✤ Not so good for CPU bound tasks
Using greenlets ✤ We need something that will know which
greenlet should run next ✤ Our calls must not block ✤ We need something to notify us when our call is done
Using greenlets ✤ We need something that will know which
greenlet should run next ✤ Our calls must not block ✤ We need something to notify us when our call is done Scheduler
Using greenlets ✤ We need something that will know which
greenlet should run next ✤ Our calls must not block ✤ We need something to notify us when our call is done Scheduler Event loop
Event loop ✤ Listens for events from OS and notifies
your app ✤ Asynchronous
None
✤ Scheduler ✤ Event loop Greenlets + ...
Gevent
Gevent ✤ “...coroutine-based Python networking library that uses greenlet to
provide a high-level synchronous API on top of the libevent event loop.”
None
Prog 1 ✤ Sleep 200ms # of threads 100 1K
10K 100K Time 207 ms 327 ms 2.55 s 25.42 s # of Greenlets 100 1K 10K 100K Time 204 ms 223 ms 421 ms 3.06 s
Prog 2 ✤ Sleep 90s # of threads 100 1K
10K 100K RAM 4.9 GB 11.8 GB 82GB ? (256GB) # of Greenlets 100 1K 10K 100K Time 33 MB 41 MB 114 MB 858 MB
Gevent ✤ Monkey-patching ✤ Event loop
Disadvantages ✤ Monkey-patching ✤ Doesn’t work with C extensions ✤
Greenlet implementation details ✤ Hard to debug
Alternatives ✤ Twisted ✤ Tornado ✤ Callback based
PEP 3156 & Tulip ✤ Attempt to standardize event loop
API in Python ✤ Tulip is an implementation
Recap ✤ Concurrent execution helps with IO bound applications ✤
Use threads if it works for you ✤ Use async library if you have lots of connections
Thank you! ✤ Questions?
Resources ✤ http:/ /dabeaz.com/coroutines/Coroutines.pdf ✤ http:/ /www.gevent.org/ ✤ http:/ /greenlet.readthedocs.org/en/latest/