Asynchronous Requests
Description#
One benefit of server-side integration is that you can leverage the huge bandwidth and computing resources available on the server-side by using parallelism. Target Python SDK supports asynchronous requests, which can reduce the effective target time to zero.
Supported Methods#
Python
Copied to your clipboard1get_offers(options)2send_notifications(options)3get_attributes(mbox_names, options)
Example#
A sample application that uses the asyncio
module's async/await in Python 3.9+ could look like this:
Python
Copied to your clipboard1async def execute_mboxes(self, mboxes):2 context = Context(channel=ChannelType.WEB)3 execute = ExecuteRequest(mboxes=mboxes)4 delivery_request = DeliveryRequest(context=context, execute=execute)56 get_offers_options = {7 "request": delivery_request8 }9 return await asyncio.to_thread(target_client.get_offers, get_offers_options)1011async def get_target_delivery_response(mboxes):12 target_delivery_response = await execute_mboxes(mboxes)13 response = Response(target_delivery_response.get("response").to_str(), status=200, mimetype='application/json')14 return response1516mboxes = [MboxRequest(name="a1-serverside-ab", index=1)]17return asyncio.run(get_target_delivery_response(mboxes)
This example assumes you you are using Python 3.9+. If using an older version of Python you can still send asynchronous requests by passing in options.callback
to get_offers
. Check out the sample Flask app for more details about asynchronous execution using either callbacks or async/await, here.