非同期リクエスト (Python)

説明

サーバーサイド統合の利点の1つは、並列処理を使用して、サーバーサイドで利用可能な巨大な帯域幅とコンピューティングリソースを活用できることです。Target Python SDKは非同期リクエストをサポートしており、これにより効果的な目標時間をゼロにすることができます。

サポートされるメソッド

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を使用している場合でも、options.callbackをget_offersに渡すことで非同期リクエストを送信できます。 コールバックまたは非同期/待機を使用した非同期実行の詳細については、サンプル Flask アプリを参照してください。こちらこちら。

recommendation-more-help
target-dev-help-dev