Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

Александр Жилин. Асинхронный Python глазами нов...

Александр Жилин. Асинхронный Python глазами новичка

Я расскажу об особенностях асинхронного программирования на Python 3 и о своем опыте разработки микросервисов на Tornado и asyncio.

Python Community Chelyabinsk

October 21, 2017
Tweet

More Decks by Python Community Chelyabinsk

Other Decks in Programming

Transcript

  1. 2

  2. 3

  3. 4

  4. 5

  5. 6

  6. 7

  7. 8

  8. 9

  9. 10 async def foo(): return "Hello, world! I'm asynchronous.“ async

    def bar(): return "Hi! I'm asynchronous too."
  10. 11

  11. 12 import asyncio async def foo(): await asyncio.sleep(100500) return 'DATA'

    async def bar(): foo_result = await foo() return foo_result.lower()
  12. 13

  13. 15 async def foo(): return "Hello, world! I'm asynchronous." result

    = foo() print(result) <coroutine object foo at 0x03C18F60>
  14. 17 async def foo(): return "Hello, world! I'm asynchronous." result

    = await foo() print(result) SyntaxError: invalid syntax
  15. 18 from asyncio import get_event_loop async def foo(): return "Hello,

    world! I'm asynchronous." loop = get_event_loop() result = loop.run_until_complete(foo()) print(result)
  16. 19 from asyncio import get_event_loop async def foo(): return "Hello,

    world! I'm asynchronous." loop = get_event_loop() result = loop.run_until_complete(foo()) print(result) Hello, world! I’m asynchronous.
  17. 20 from asyncio import get_event_loop async def foo(): return "Hello,

    world! I'm asynchronous." loop = get_event_loop() result = loop.run_until_complete(foo()) print(result) Hello, world! I’m asynchronous.
  18. 21 from tornado.platform.asyncio import AsyncIOMainLoop async def foo(): return "Hello,

    world! I'm asynchronous." loop = AsyncIOMainLoop() result = loop.run_sync(foo) print(result) Hello, world! I’m asynchronous.
  19. 22 from tornado.platform.asyncio import AsyncIOMainLoop async def foo(): return "Hello,

    world! I'm asynchronous." loop = AsyncIOMainLoop() result = loop.run_sync(foo) print(result) Hello, world! I’m asynchronous.
  20. 23 from tornado.platform.asyncio import AsyncIOMainLoop from functools import partial async

    def bar(username): return f"Hello, {username}! I'm asynchronous." loop = AsyncIOMainLoop() result = loop.run_sync(partial(bar, 'Vasya')) print(result) Hello, Vasya! I’m asynchronous.
  21. 24 from tornado.platform.asyncio import AsyncIOMainLoop from functools import partial async

    def bar(username): return f"Hello, {username}! I'm asynchronous." loop = AsyncIOMainLoop() result = loop.run_sync(partial(bar, 'Vasya')) print(result) Hello, Vasya! I’m asynchronous.
  22. 25

  23. 26 from mock import patch, mock import time def foo():

    time.sleep(100500) return 'DATA' def bar(): foo_result = foo() return foo_result.lower() ... def test_bar(): patch('foo', new=mock.MagicMock(return_value='DATA')) assert bar() == 'data'
  24. 27 from mock import patch, mock import time def foo():

    time.sleep(100500) return 'DATA' def bar(): foo_result = foo() return foo_result.lower() ... def test_bar(): patch('foo', new=mock.MagicMock(return_value='DATA')) assert bar() == 'data'
  25. 28 from mock import patch, mock import time def foo():

    time.sleep(100500) return 'DATA' def bar(): foo_result = foo() return foo_result.lower() ... def test_bar(): patch('foo', new=mock.MagicMock(return_value='DATA')) assert bar() == 'data'
  26. 29 from mock import patch, mock import pytest import asyncio

    async def foo(): await asyncio.sleep(100500) return 'DATA' async def bar(): foo_result = await foo() return foo_result.lower() ... async def test_bar(): patch('foo', new=mock.MagicMock(return_value='DATA')) assert await bar() == 'data'
  27. 30 from mock import mock def async_mock(*args, **kwargs): m =

    mock.MagicMock(*args, **kwargs) async def mock_coroutine(*inner_args, **inner_kwargs): return m(*inner_args, **inner_kwargs) mock_coroutine.mock = m return mock_coroutine
  28. 31 from mock import patch, mock import pytest def async_mock(*args,

    **kwargs): m = mock.MagicMock(*args, **kwargs) async def mock_coroutine(*inner_args, **inner_kwargs): return m(*inner_args, **inner_kwargs) mock_coroutine.mock = m return mock_coroutine async def test_bar(): patch('foo', new=async_mock(return_value='DATA')) assert await bar() == 'data'
  29. 32 from mock import patch, mock import pytest def async_mock(*args,

    **kwargs): m = mock.MagicMock(*args, **kwargs) async def mock_coroutine(*inner_args, **inner_kwargs): return m(*inner_args, **inner_kwargs) mock_coroutine.mock = m return mock_coroutine async def test_bar(): patch('foo', new=async_mock(return_value='DATA')) assert await bar() == 'data'
  30. 33