Slide 1

Slide 1 text

Jyrki Pulliainen / 18.10.2011 / Going concurrent asynchronous with Python Wednesday, October 19, 2011

Slide 2

Slide 2 text

Going asynchronous with Python / Motivation • Not threaded • Good for I/O bound software • Buzzword-compatible! Wednesday, October 19, 2011

Slide 3

Slide 3 text

Going asynchronous with Python / Two ways to achieve • Coroutines • Event loop Wednesday, October 19, 2011

Slide 4

Slide 4 text

Going asynchronous with Python / Coroutines • Components, that are suspendable / resumable • One scheduler to rule them all Wednesday, October 19, 2011

Slide 5

Slide 5 text

Going asynchronous with Python / Coroutines in Python • Python 2.5 added support for coroutines >>>def coroutine(): >>> input = yield i >>> print i + 1 >>> a = coroutine() >>> a.next() >>> a.send(1) 2 Wednesday, October 19, 2011

Slide 6

Slide 6 text

Going asynchronous with Python / Running multiple coroutines def main_loop(self): while self.tasks_left(): task = self.get_next_task() try: task.send(None) except StopIteration: # Do not schedule if the task is done self.remove_task(task) Wednesday, October 19, 2011

Slide 7

Slide 7 text

Going asynchronous with Python / Event loop • Good for I/O • Main idea: Wait for something to happen, then pass to a handler Wednesday, October 19, 2011

Slide 8

Slide 8 text

Going asynchronous with Python / Idea in a nutshell class EventLoop(object): # ... def main_loop(): while True: event = self.wait_for_event() handler = self.get_handler_for_event(handler) handler() Wednesday, October 19, 2011

Slide 9

Slide 9 text

Going asynchronous with Python / Finding events • Usually you want to poll I/O • Tools for that: select, kselect, epoll... Wednesday, October 19, 2011

Slide 10

Slide 10 text

Jyrki Pulliainen / 18.10.2011 / CODE TIEM!!11 (actually, just looking at it) Wednesday, October 19, 2011