Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Going asynchronous with Python

Going asynchronous with Python

Slides from my joint workshop at PyCon Finland 2011

Jyrki Pulliainen

October 19, 2011
Tweet

More Decks by Jyrki Pulliainen

Other Decks in Technology

Transcript

  1. Going asynchronous with Python / Motivation • Not threaded •

    Good for I/O bound software • Buzzword-compatible! Wednesday, October 19, 2011
  2. Going asynchronous with Python / Two ways to achieve •

    Coroutines • Event loop Wednesday, October 19, 2011
  3. Going asynchronous with Python / Coroutines • Components, that are

    suspendable / resumable • One scheduler to rule them all Wednesday, October 19, 2011
  4. 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
  5. 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
  6. 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
  7. 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
  8. Going asynchronous with Python / Finding events • Usually you

    want to poll I/O • Tools for that: select, kselect, epoll... Wednesday, October 19, 2011