Mini PyCon MY 2014 March 2014, Kuala Lumpur
Concurrency In PythonFarzad Ghaneiwww.ghanei.net
View Slide
!= Parallel computingConcurrency== Handling tasks while others are still running
Blocked by IOWhy?Processing takes long
ThreadsSolutionsProcessesCoroutines
Code Outline
Simple, sync IO APIThreadsPreemptive scheduling by OSUnpredictable execution ordering
IO bound threads
CPU bound threads
Threads release and acquire GILGlobal Interpreter LockThe one thread that has GIL runs at a time
1 CPU bound thread
3 CPU bound threads
Not good for CPU boundThreadsGood for user Interactivity, maybe IOShared state might cause difficulties
Simple, sync IO APIProcessesPreemptive scheduling by OSUnpredictable execution ordering
IO bound processes
CPU bound processes
ProcessesGood for parallel processingUse more resources than threadsShared state might cause difficulties
Predictable execution orderingCoroutinesCooperative concurrency
PEP 380 (yield from)CoroutinesGenerators (yield)Greenlets (green threads)
CPU bound greenlets
CoroutinesHave early resultsControlling execution
Async IONo blocking callsRespond to IO Events
Event driven concurrencyCallback based (Twisted)Coroutine based (Gevent)Both (asyncio)
GeventImplicit coroutines (using greenlets)threading like APIMonkey patchinglibev event loop
Gevent
TwistedExplicit event loopBatteries includedDeferredsNo blocking calls
Twisted
asyncio (tulip)PEP 3156 (asyncio)Callbacks and coroutines APIStandard libraryExplicit event loop
Thanks