Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anterior Revisión previa Próxima revisión | Revisión previa | ||
|
wiki2:python:notes [2020/06/20 13:46] alfred [Advanced asyncio] |
wiki2:python:notes [2022/01/15 08:41] (actual) |
||
|---|---|---|---|
| Línea 78: | Línea 78: | ||
| ===== Advanced asyncio ===== | ===== Advanced asyncio ===== | ||
| + | |||
| + | For a basic use go to [[wiki2:python3#asyncio_package|asyncio package]]. | ||
| + | |||
| + | A good video about advanced asyncio: https://youtu.be/sW76-pRkZk8 | ||
| + | ==== asyncio perks ==== | ||
| + | |||
| + | You can store the function and call it later: | ||
| + | <code python> | ||
| + | import asyncio | ||
| + | |||
| + | async def speak(): | ||
| + | print('C') | ||
| + | await asyncio.sleep(3) | ||
| + | return 'D' | ||
| + | |||
| + | async def run(): | ||
| + | will_speak = speak() | ||
| + | print('A') | ||
| + | print('B') | ||
| + | print(await will_speak) | ||
| + | |||
| + | loop = asyncio.get_event_loop() | ||
| + | loop.run_until_complete(run()) | ||
| + | # Prints: A B C D | ||
| + | </code> | ||
| + | |||
| + | You can wait until all the co-routines are finished: | ||
| + | <code python> | ||
| + | async def run(): | ||
| + | f = [] | ||
| + | for x in range(1, 6): | ||
| + | f.append(meow(x)) | ||
| + | await asyncio.wait(f) | ||
| + | </code> | ||
| + | |||
| + | ==== Testing ==== | ||
| + | === Mocking co-routines === | ||
| + | <code python> | ||
| + | from unittest.mock import AsyncMock | ||
| + | |||
| + | @pytest.fixture() | ||
| + | def mock_sum(mocker): | ||
| + | async_mock = AsyncMock() | ||
| + | mocker.patch('app.sum', side_effect=async_mock) | ||
| + | return async_mock | ||
| + | |||
| + | @pytest.mark.asyncio | ||
| + | async def test_sum(mock_sum): | ||
| + | mock_sum.return_value = 4 | ||
| + | result = await app.sum(1, 2) | ||
| + | assert result == 4 | ||
| + | </code> | ||
| + | |||
| + | ==== asyncio on Django ==== | ||
| + | === django-simple-task package === | ||
| + | * https://pypi.org/project/django-simple-task/ | ||
| + | <code python> | ||
| + | from django_simple_task import defer | ||
| + | |||
| + | def task1(): | ||
| + | time.sleep(1) | ||
| + | print("task1 done") | ||
| + | |||
| + | async def task2(): | ||
| + | await asyncio.sleep(1) | ||
| + | print("task2 done") | ||
| + | |||
| + | def view(requests): | ||
| + | defer(task1) | ||
| + | defer(task2) | ||
| + | return HttpResponse(b"My View") | ||
| + | </code> | ||
| + | |||
| ==== Libraries ==== | ==== Libraries ==== | ||
| * [[https://docs.aiohttp.org/en/stable/|AIOHTTP]], a ''requests'' async alternative. | * [[https://docs.aiohttp.org/en/stable/|AIOHTTP]], a ''requests'' async alternative. | ||
| + | |||
| + | |||
| + | ===== Black ===== | ||
| + | |||
| + | Launch black for those .py file which are gonna be commited: | ||
| + | <code> | ||
| + | git diff --name-only | grep ".py$" | xargs black | ||
| + | </code> | ||
| + | ===== Notes ===== | ||
| + | ==== Install a new version of python ==== | ||
| + | |||
| + | To install a new version of python do: | ||
| + | <code> | ||
| + | sudo apt-get install python3.8 python3.8-dev python3.8-distutils python3.8-venv | ||
| + | </code> | ||
| + | |||
| + | ==== try\except\finally perks ==== | ||
| + | |||
| + | This code... | ||
| + | <code python> | ||
| + | def should_rise(flag): | ||
| + | if flag: | ||
| + | raise Exception() | ||
| + | |||
| + | def my_function(flag): | ||
| + | try: | ||
| + | should_rise(flag) | ||
| + | print("1") | ||
| + | return "1" | ||
| + | except: | ||
| + | print("2") | ||
| + | return "2" | ||
| + | finally: | ||
| + | print("3") | ||
| + | return "3" | ||
| + | |||
| + | |||
| + | print("f", my_function(False)) | ||
| + | print("---") | ||
| + | print("f", my_function(True)) | ||
| + | </code> | ||
| + | |||
| + | Prints: | ||
| + | <code> | ||
| + | 1 | ||
| + | 3 | ||
| + | f 3 | ||
| + | --- | ||
| + | 2 | ||
| + | 3 | ||
| + | f 3 | ||
| + | </code> | ||
| + | |||
| + | ==== Install package for the user ==== | ||
| + | <code> | ||
| + | pip install --user mypy | ||
| + | </code> | ||
| + | |||
| + | Executables are found in ''~/.local/bin'' so you can do the next: | ||
| + | <code> | ||
| + | export PATH="$PATH:$HOME/.poetry/bin:$HOME/bin:$HOME/.local/bin" | ||
| + | </code> | ||