Notice: file_put_contents(): Write of 17401 bytes failed with errno=28 No space left on device in /var/www/tgoop/post.php on line 50
Zen of Python@zen_of_python P.4053
ZEN_OF_PYTHON Telegram 4053
Вопрос подписчика

Задает @AlekseiKiselev:

«А вопрос то и назрел, как по правильному делать асинхронный скоуп в алхимии.... На ум пришел синглтон, но не понятно будет-ли течь память, так как закрытие сессии явно приводит к открытию новой, а это дико медленно, поэтому сессию закрывать крайне не охота. Может есть какие-то бест практис?

Пример, что может пойти не так?


class Db:
session: async_scoped_session

def __new__(cls, config: RelationDatabaseConfig):
if not hasattr(cls, 'instance'):
cls.instance = super(Db, cls).__new__(cls)

return cls.instance

def __init__(self, config: RelationDatabaseConfig):
# Создаем коннект к БД
self._async_engine = create_async_engine(
config.connection_string,
echo=False,
pool_pre_ping=True,
poolclass=NullPool
)

# Создаем фабрику для создания сессии
self._async_session_factory = async_sessionmaker(
self._async_engine,
expire_on_commit=False,
)

# Создаем сессию на основе фабрики
self.session = async_scoped_session(
self._async_session_factory,
scopefunc=asyncio.current_task
)


Пожалуйста, будьте взаимовежливы. Однажды и вам помогут в этой рубрике.

#обсуждение

@zen_of_python
👍2



tgoop.com/zen_of_python/4053
Create:
Last Update:

Вопрос подписчика

Задает @AlekseiKiselev:

«А вопрос то и назрел, как по правильному делать асинхронный скоуп в алхимии.... На ум пришел синглтон, но не понятно будет-ли течь память, так как закрытие сессии явно приводит к открытию новой, а это дико медленно, поэтому сессию закрывать крайне не охота. Может есть какие-то бест практис?

Пример, что может пойти не так?


class Db:
session: async_scoped_session

def __new__(cls, config: RelationDatabaseConfig):
if not hasattr(cls, 'instance'):
cls.instance = super(Db, cls).__new__(cls)

return cls.instance

def __init__(self, config: RelationDatabaseConfig):
# Создаем коннект к БД
self._async_engine = create_async_engine(
config.connection_string,
echo=False,
pool_pre_ping=True,
poolclass=NullPool
)

# Создаем фабрику для создания сессии
self._async_session_factory = async_sessionmaker(
self._async_engine,
expire_on_commit=False,
)

# Создаем сессию на основе фабрики
self.session = async_scoped_session(
self._async_session_factory,
scopefunc=asyncio.current_task
)


Пожалуйста, будьте взаимовежливы. Однажды и вам помогут в этой рубрике.

#обсуждение

@zen_of_python

BY Zen of Python




Share with your friend now:
tgoop.com/zen_of_python/4053

View MORE
Open in Telegram


Telegram News

Date: |

Add up to 50 administrators It’s easy to create a Telegram channel via desktop app or mobile app (for Android and iOS): Deputy District Judge Peter Hui sentenced computer technician Ng Man-ho on Thursday, a month after the 27-year-old, who ran a Telegram group called SUCK Channel, was found guilty of seven charges of conspiring to incite others to commit illegal acts during the 2019 extradition bill protests and subsequent months. With the administration mulling over limiting access to doxxing groups, a prominent Telegram doxxing group apparently went on a "revenge spree." Done! Now you’re the proud owner of a Telegram channel. The next step is to set up and customize your channel.
from us


Telegram Zen of Python
FROM American