BOOKPYTHON Telegram 3721
Начиная с Python 3.7, модуль contextlib предоставляет декоратор asynccontextmanager, который позволяет определять асинхронные контекстные менеджеры точно так же, как contextmanager:


import asyncio
from contextlib import asynccontextmanager

@asynccontextmanager
async def slow(delay):
half = delay / 2
await asyncio.sleep(half)
yield
await asyncio.sleep(half)

async def main():
async with slow(1):
print('slow')

loop = asyncio.get_event_loop()
loop.run_until_complete(main())


Для более старых версий Python можно использовать @asyncio_extras.async_contextmanager.

👉@BookPython
👍4



tgoop.com/BookPython/3721
Create:
Last Update:

Начиная с Python 3.7, модуль contextlib предоставляет декоратор asynccontextmanager, который позволяет определять асинхронные контекстные менеджеры точно так же, как contextmanager:


import asyncio
from contextlib import asynccontextmanager

@asynccontextmanager
async def slow(delay):
half = delay / 2
await asyncio.sleep(half)
yield
await asyncio.sleep(half)

async def main():
async with slow(1):
print('slow')

loop = asyncio.get_event_loop()
loop.run_until_complete(main())


Для более старых версий Python можно использовать @asyncio_extras.async_contextmanager.

👉@BookPython

BY Библиотека Python разработчика | Книги по питону


Share with your friend now:
tgoop.com/BookPython/3721

View MORE
Open in Telegram


Telegram News

Date: |

Select “New Channel” To edit your name or bio, click the Menu icon and select “Manage Channel.” In the next window, choose the type of your channel. If you want your channel to be public, you need to develop a link for it. In the screenshot below, it’s ”/catmarketing.” If your selected link is unavailable, you’ll need to suggest another option. Hashtags Don’t publish new content at nighttime. Since not all users disable notifications for the night, you risk inadvertently disturbing them.
from us


Telegram Библиотека Python разработчика | Книги по питону
FROM American