非同期要求(Python)
最終更新日: 2024年7月22日
- トピック:
- APIs/SDKs
作成対象:
- 開発者
説明
サーバーサイド統合の利点の 1 つは、並列処理を使用することで、サーバーサイドで利用できる膨大な帯域幅とコンピューティングリソースを活用できる点です。 Python SDKTarget、非同期要求をサポートしているので、有効なターゲット時間をゼロに減らすことができます。
サポートされるメソッド
Python
get_offers(options)
send_notifications(options)
get_attributes(mbox_names, options)
例
Python 3.9+で asyncio
モジュールの async/await を使用するサンプルアプリケーションは、次のようになります。
Python
async def execute_mboxes(self, mboxes):
context = Context(channel=ChannelType.WEB)
execute = ExecuteRequest(mboxes=mboxes)
delivery_request = DeliveryRequest(context=context, execute=execute)
get_offers_options = {
"request": delivery_request
}
return await asyncio.to_thread(target_client.get_offers, get_offers_options)
async def get_target_delivery_response(mboxes):
target_delivery_response = await execute_mboxes(mboxes)
response = Response(target_delivery_response.get("response").to_str(), status=200, mimetype='application/json')
return response
mboxes = [MboxRequest(name="a1-serverside-ab", index=1)]
return asyncio.run(get_target_delivery_response(mboxes)
この例では、Python 3.9 以降を使用していることを前提としています。 古いバージョンの Python を使用している場合でも、get_offers
に options.callback
を渡すことで非同期リクエストを送信できます。 コールバックまたは async/await を使用した非同期実行について詳しくは、サンプル Flask アプリを参照してください こちら。
recommendation-more-help
6906415f-169c-422b-89d3-7118e147c4e3