Created
March 21, 2021 15:30
-
-
Save thanakijwanavit/394830a2395330b0a07db1fd4f69cc46 to your computer and use it in GitHub Desktop.
async await example in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests, time, random | |
import asyncio | |
from asgiref.sync import sync_to_async | |
from datetime import datetime | |
async def post(url, body): | |
t0 = datetime.now() | |
r = await sync_to_async(requests.post)(url,json=body) | |
return r.json() | |
async def main(): | |
url = 'https://hkf9vqpqe4.execute-api.ap-southeast-1.amazonaws.com/Prod/generate' | |
body = {"branchId" : "test"} | |
r = await asyncio.gather(*(post(url, body) for i in range(100))) | |
return r | |
await main() # in jupyter notebook | |
if __name__ == "__main__": # using command line execution | |
asyncio.run(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment