0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

背景

以前やってた際は、Flask だったが、Quart 移行となった際に、躓いた記録

import 変更

同期
from azure.cosmos import CosmosClient
非同期用へ
from azure.cosmos.aio import CosmosClient

取得方法の変更

以前の一例
next_item = next(query_items)
非同期用へ
query_items_response = container.query_items(
    query="SELECT * FROM r WHERE r.id=@id",
    parameters=[
        {"name": "@id", "value": doc_id}
    ]
)

items = [item async for item in query_items_response]

エラー対処

hypercorn.utils.LifespanFailureError: Lifespan failure in startup. ''DatabaseProxy' object has no attribute 'get_database_client''

え?あるよ、と思ったけど、非同期化したことで、メソッドチェーンを見直さないとダメって話。
Container Client も同様だと思われる。AsyncItemPaged[] なので

同期
cosmos_client = CosmosClient(
    url=AZURE_COSMOSDB_ENDPOINT,
    credential=azure_credential
    ).get_database_client(
        AZURE_COSMOSDB_DATABASE)
非同期
cosmos_client = CosmosClient(
    url=AZURE_COSMOSDB_ENDPOINT,
    credential=azure_credential
    )
database_client = cosmos_client.get_database_client(
        AZURE_COSMOSDB_DATABASE)

TypeError: ClientSession._request() got an unexpected keyword argument 'enable_cross_partition_query'

aio 用では、対応してないってだけなので、削除すればOK。

必要なら partition_key を検討すれば良さそう

Flask
items = container_alarm.query_items(query=sql_query, enable_cross_partition_query=True)
Quart
items = container_alarm.query_items(query=sql_query)

あとがき

非同期を利用したことはあっても、非同期化をしたことがなかったので、なんか不安しかない・・ :sweat:

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?