webclinic017 / python-async-concurrent-requests

A simple python script that allows for concurrent post requests by using async and await functionality with the help of the asyncio and aiohttp modules.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

python-async-concurrent-requests

A simple python script that allows for concurrent post requests by using async and await functionality with the help of the asyncio and aiohttp modules. I am using python version 3.7.

Asyncio is native to python after version 3.5 so there is no need to do a pip install if you are running python at that version or higher but aiohttp does have to be pip installed. The concurrent_requests.py file is the file doing the asynchronous concurrent post requests. The number of requests can be adjusted on line 122 in the run() function (currently I have it set to 10). The file server.py is a simple flask server (flask has to be pip installed) with one post route that accepts headers and a payload. Fire up the server by opening one command line window (terminal, cmd, etc), change directories to be in the directory with these files, and run python server.py. Then open up another command line window and run python concurrent_requests.py in the same directory to fire off the concurrent calls. Below is an example of this concurrent behavior. Async Concurrent Python calls

A quick backstory to why I set this simple repo up. I work as a software developer and one of our servers started giving us unexpected 502 errors every so often. This was due to an increased number of calls to that server but since it wasn't consistent we had to figure out why and how it was happening. This led to the task to developing a script that could do an X amount of calls at the same time. My coworker was able to easily set this beavior up with a newer programming language called Elixir since it is specifically designed to easily run thousands of processes at the same time. I wanted to do it in python since most of my coworkers are familiar with python. Once I had my script ready, I kept increasing the number of concurrent calls until we figured out the number that would cause our server to fail, which allowed us to make accomodations.

Test the limits of your own servers by increasing the number of calls! I used this article as a reference.

About

A simple python script that allows for concurrent post requests by using async and await functionality with the help of the asyncio and aiohttp modules.


Languages

Language:Python 100.0%