Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Greenlet-based concurrency
Search
Goran Peretin
July 03, 2013
Programming
2
560
Greenlet-based concurrency
Slides from EuroPython 2013 talk Greenlet-based concurrency.
Goran Peretin
July 03, 2013
Tweet
Share
More Decks by Goran Peretin
See All by Goran Peretin
Webcamp Zagreb 2013
gperetin
1
270
On Concurrency
gperetin
1
340
WebcampZG 2012
gperetin
1
400
Other Decks in Programming
See All in Programming
WebフロントエンドにおけるGraphQL(あるいはバックエンドのAPI)との向き合い方 / #241106_plk_frontend
izumin5210
4
1.4k
TypeScriptでライブラリとの依存を限定的にする方法
tutinoko
2
660
Webの技術スタックで マルチプラットフォームアプリ開発を可能にするElixirDesktopの紹介
thehaigo
2
1k
見せてあげますよ、「本物のLaravel批判」ってやつを。
77web
7
7.7k
「今のプロジェクトいろいろ大変なんですよ、app/services とかもあって……」/After Kaigi on Rails 2024 LT Night
junk0612
5
2.1k
Why Jakarta EE Matters to Spring - and Vice Versa
ivargrimstad
0
1k
弊社の「意識チョット低いアーキテクチャ」10選
texmeijin
5
24k
as(型アサーション)を書く前にできること
marokanatani
9
2.6k
Realtime API 入門
riofujimon
0
150
2024/11/8 関西Kaggler会 2024 #3 / Kaggle Kernel で Gemma 2 × vLLM を動かす。
kohecchi
5
910
Jakarta EE meets AI
ivargrimstad
0
580
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
1
100
Featured
See All Featured
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
Optimising Largest Contentful Paint
csswizardry
33
2.9k
Six Lessons from altMBA
skipperchong
27
3.5k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
25
1.8k
For a Future-Friendly Web
brad_frost
175
9.4k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
A Tale of Four Properties
chriscoyier
156
23k
Bash Introduction
62gerente
608
210k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
120
Agile that works and the tools we love
rasmusluckow
327
21k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
250
21k
Rails Girls Zürich Keynote
gr2m
94
13k
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/