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

Tulip и Асинхронное Программирование в Python 3

Tulip и Асинхронное Программирование в Python 3

Андрей Саломатин
Асинхронный код — просто и эффективно. Tulip позволяет писать приложения, которые используют возможности ОС для асинхронного ввода/вывода. Фреймворк станет частью Python 3.4. Доклад о том, как устроен Tulip и как им пользоваться.

Avatar for Moscow Python Meetup

Moscow Python Meetup PRO

March 27, 2014
Tweet

More Decks by Moscow Python Meetup

Other Decks in Programming

Transcript

  1. CALLBACKS def read_data_from(name): waiter = asyncio.Future() result = “Data from

    {}”.format(name) loop.call_later(.1, waiter.set_result, result) return waiter def on_data(future): print(future.result()) loop.stop() def read_and_process(): read_data_from('file').add_done_callback(on_data) loop.call_soon(read_and_process) loop.run_forever()
  2. COROUTINES def coro_func(): print("Before yield") (yield) print("After yield") return 'coroutine

    result' coro = coro_func() next(coro) # Before yield next(coro) # After yield
  3. COROUTINES @asyncio.coroutine def coro_func(): print("Before yield") (yield) print("After yield") return

    'coroutine result' coro = coro_func() next(coro) # Before yield next(coro) # After yield, StopIteration
  4. COROUTINES @asyncio.coroutine def coro_usage(): result = yeild from coro_func() print(result)

    coro = coro_usage() next(coro) # Before yield next(coro) # After yield # coroutine result # StopIteration
  5. # read one source, callbacks def on_data(future): print(future.result()) loop.stop() def

    read_and_process(): future = read_data_from(’file’) future.add_done_callback(on_data) loop.call_soon(read_and_process) loop.run_forever() CALLBACKS
  6. # read one source, coroutines @asyncio.coroutine def read_and_process(): future =

    read_data_from('file') result = yield from future print(result) loop.run_until_complete(read_and_process()) COROUTINES
  7. # read two sources sequentially, callbacks def on_data_2(future2): print("Content2: %s"

    % future2.result()) loop.stop() def on_data_1(future1): print("Content1: %s\n" % future1.result()) future2 = read_data_from('file2') future2.add_done_callback(on_data_2) def read_and_process(): future1 = read_data_from('file1') future1.add_done_callback(on_data_1) loop.call_soon(read_and_process) loop.run_forever() CALLBACKS
  8. # read two sources sequentially, coroutines def read_and_process(): future1 =

    read_data_from('file1') content1 = yield from future1 print("Content1: %s" % content1) future2 = read_data_from('file2') content2 = yield from future2 print("Content2: %s" % content1) loop.run_until_complete(read_and_process()) COROUTINES
  9. # read two sources in parallel, callbacks def on_data_2(future): print("Content2:

    %s\n" % future.result()) # loop.stop() def on_data_1(future): print("Content1: %s\n" % future.result()) # loop.stop() def read_and_process(): future1 = read_data_from('file1') future2 = read_data_from('file2') future1.add_done_callback(on_data_1) future2.add_done_callback(on_data_2) loop.call_soon(read_and_process) loop.run_forever() CALLBACKS
  10. # read two sources in parallel, coroutines def read_and_process(): content

    = asyncio.gather( read_data_from('file1'), read_data_from('file2')) content1, content2 = yield from content print( "Content1: %s\nContent2: %s\n" % \ (content1, content2)) loop.run_until_complete(read_and_process()) COROUTINES
  11. koa.py middleware @asyncio.coroutine def log_headers(ctx, nxt): print(ctx.request.headers) yield from nxt

    @asyncio.coroutine def say_hello(ctx, nxt): ctx.response.status = 200 ctx.response.write('Hello') yield from nxt