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

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

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

49c3bfded3cf5f5100ef423140676288?s=128

Python Community Chelyabinsk

October 21, 2017
Tweet

Transcript

  1. None
  2. 2

  3. 3

  4. 4

  5. 5

  6. 6

  7. 7

  8. 8

  9. 9

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

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

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

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

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

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

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

    = await foo() print(result)
  17. 17 async def foo(): return "Hello, world! I'm asynchronous." result

    = await foo() print(result) SyntaxError: invalid syntax
  18. 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)
  19. 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.
  20. 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.
  21. 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.
  22. 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.
  23. 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.
  24. 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.
  25. 25

  26. 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'
  27. 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'
  28. 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'
  29. 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'
  30. 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
  31. 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'
  32. 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'
  33. 33

  34. None