我遇到了一种情况-我在python3.x中使用asyncio
包,并将数据持久化到with
块中,类似这样:
test_repo = TestRepository()
with (yield from test_repo):
res = yield from test_repo.get_by_lim_off(
page_size=int(length),
offset=start,
customer_name=customer_name,
customer_phone=customer_phone,
return_type=return_type
)
我需要在with
块中获取res
数据,但是当我退出with
块时,应该发生持久化和获取数据的操作。我怎样才能做到这一点?你知道吗
只有在Python 3.5+中,通过异步上下文管理器(
__aenter__
/__aexit__
)和async with
才支持此行为,这两种管理器都添加在PEP 492:在3.5之前,必须使用
try
/finally
块来显式调用init/cleanup协程,不幸的是:相关问题 更多 >
编程相关推荐