BOOKPYTHON Telegram 3719
Если ты хочешь, чтобы контекстный менеджер при входе или выходе из контекста приостанавливал выполнение корутины, следует использовать асинхронные контекстные менеджеры. Вместо вызова m.__enter__() и m.__exit__() Python в этом случае выполняет await m.__aenter__() и await m.__aexit__() соответственно.

Асинхронные контекстные менеджеры нужно использовать с синтаксисом async with:


import asyncio

class Slow:
def __init__(self, delay):
self._delay = delay

async def __aenter__(self):
await asyncio.sleep(self._delay / 2)

async def __aexit__(self, *exception):
await asyncio.sleep(self._delay / 2)

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

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


В этом примере класс Slow симулирует задержку при входе и выходе из контекста.

👉@BookPython
👍31



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

Если ты хочешь, чтобы контекстный менеджер при входе или выходе из контекста приостанавливал выполнение корутины, следует использовать асинхронные контекстные менеджеры. Вместо вызова m.__enter__() и m.__exit__() Python в этом случае выполняет await m.__aenter__() и await m.__aexit__() соответственно.

Асинхронные контекстные менеджеры нужно использовать с синтаксисом async with:


import asyncio

class Slow:
def __init__(self, delay):
self._delay = delay

async def __aenter__(self):
await asyncio.sleep(self._delay / 2)

async def __aexit__(self, *exception):
await asyncio.sleep(self._delay / 2)

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

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


В этом примере класс Slow симулирует задержку при входе и выходе из контекста.

👉@BookPython

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


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

View MORE
Open in Telegram


Telegram News

Date: |

During the meeting with TSE Minister Edson Fachin, Perekopsky also mentioned the TSE channel on the platform as one of the firm's key success stories. Launched as part of the company's commitments to tackle the spread of fake news in Brazil, the verified channel has attracted more than 184,000 members in less than a month. The optimal dimension of the avatar on Telegram is 512px by 512px, and it’s recommended to use PNG format to deliver an unpixelated avatar. The channel also called on people to turn out for illegal assemblies and listed the things that participants should bring along with them, showing prior planning was in the works for riots. The messages also incited people to hurl toxic gas bombs at police and MTR stations, he added. In 2018, Telegram’s audience reached 200 million people, with 500,000 new users joining the messenger every day. It was launched for iOS on 14 August 2013 and Android on 20 October 2013. 2How to set up a Telegram channel? (A step-by-step tutorial)
from us


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