concurrency in python

21
Intro Threads GIL Processes Async Concurrency in Python Konrad Delong March 23 2010, Amsterdam Konrad Delong Concurrency in Python

Upload: konryd

Post on 15-Jul-2015

2.735 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Concurrency in Python

IntroThreads

GILProcesses

Async

Concurrency in Python

Konrad Delong

March 23 2010, Amsterdam

Konrad Delong Concurrency in Python

Page 2: Concurrency in Python

IntroThreads

GILProcesses

Async

me

IT student (Erasmus)

Python

Thesis

Konrad Delong Concurrency in Python

Page 3: Concurrency in Python

IntroThreads

GILProcesses

Async

concurrency

In computer science, concurrency is a property of systems in whichseveral computations are executing simultaneously, andpotentially interacting with each other. The computations may beexecuting on multiple cores in the same chip, preemptivelytime-shared threads on the same processor, or executed onphysically separated processors.(en.wikipedia.org)

Konrad Delong Concurrency in Python

Page 4: Concurrency in Python

IntroThreads

GILProcesses

Async

threads

thread

threading

Konrad Delong Concurrency in Python

Page 5: Concurrency in Python

IntroThreads

GILProcesses

Async

thread

import threadthread.start_new_thread(function, args[, kwargs])thread.get_ident()thread.allocate_lock()

Konrad Delong Concurrency in Python

Page 6: Concurrency in Python

IntroThreads

GILProcesses

Async

threading

from threading import (Thread, Condition,Lock, RLock, Semaphore)

class MyThread(Thread):def run(self)

passt = MyThread()# ort = Thread(target=function, args=[], kwargs={})# and thent.start()t.join()

Konrad Delong Concurrency in Python

Page 7: Concurrency in Python

IntroThreads

GILProcesses

Async

GIL

Konrad Delong Concurrency in Python

Page 8: Concurrency in Python

IntroThreads

GILProcesses

Async

How it works?

Interpreter in only one thread

Released every check (== 100 ticks) and on blockingoperations

Konrad Delong Concurrency in Python

Page 9: Concurrency in Python

IntroThreads

GILProcesses

Async

check

if (--_Py_Ticker < 0) {_Py_Ticker = _Py_CheckInterval; // default 100if (interpreter_lock) {

/* Give another thread a chance */PyThread_release_lock(interpreter_lock);/* Other threads may run now */PyThread_acquire_lock(interpreter_lock, 1);

}}

Konrad Delong Concurrency in Python

Page 10: Concurrency in Python

IntroThreads

GILProcesses

Async

Why GIL is bad?

No gains on multicore

Latency in responding to events

Konrad Delong Concurrency in Python

Page 11: Concurrency in Python

IntroThreads

GILProcesses

Async

Why GIL is not that bad?

Performance on single core

Safe environment for C extentions

Other solutions than threads exist

Konrad Delong Concurrency in Python

Page 12: Concurrency in Python

IntroThreads

GILProcesses

Async

Remove GIL?

Greg Stein, python-safethread

PyPy, Unladen Swallow

current policy on dropping GIL in cPython

Konrad Delong Concurrency in Python

Page 13: Concurrency in Python

IntroThreads

GILProcesses

Async

Other solutions?

Processes instead of threads

Jython/IronPython

Async

Konrad Delong Concurrency in Python

Page 14: Concurrency in Python

IntroThreads

GILProcesses

Async

multiprocessing module

from multiprocessing import (Process, Condition,Lock, RLock, Semaphore)

class MyProcess(Process):def run(self)

passt = MyProcess()# ort = Process(target=function, args=[], kwargs={})# and thent.start()t.join()

Konrad Delong Concurrency in Python

Page 15: Concurrency in Python

IntroThreads

GILProcesses

Async

just like ...

from threading import (Thread, Condition,Lock, RLock, Semaphore)

class MyThread(Thread):def run(self)

passt = MyThread()# ort = Thread(target=function, args=[], kwargs={})# and thent.start()t.join()

Konrad Delong Concurrency in Python

Page 16: Concurrency in Python

IntroThreads

GILProcesses

Async

not entirely the same

no implicit shared memory (global vars, imported modules,other)

IPC (serializable data)

separate machines IPC

Konrad Delong Concurrency in Python

Page 17: Concurrency in Python

IntroThreads

GILProcesses

Async

Queues

def foo(queue):print queue.get()

q = Queue()t = Process(target=foo, args=[q])t.start()q.put([1, "a"])

# will print [1, "a"]

Konrad Delong Concurrency in Python

Page 18: Concurrency in Python

IntroThreads

GILProcesses

Async

Shared memory

from multiprocessing import Value, Array

num = Value(’d’, 0.0)arr = Array(’i’, range(10))

Konrad Delong Concurrency in Python

Page 19: Concurrency in Python

IntroThreads

GILProcesses

Async

Async

callbacks

no blocking

still single CPU, but better scaling (memory management)

explicit(Twisted, Tornado) vs hidden

Konrad Delong Concurrency in Python

Page 20: Concurrency in Python

IntroThreads

GILProcesses

Async

Reactor

Konrad Delong Concurrency in Python

Page 21: Concurrency in Python

IntroThreads

GILProcesses

Async

Hidden async

Kamaelia

select, epoll, kqueue

Eventlet, Gevent, PyEvent

Konrad Delong Concurrency in Python