Async HTTP Test
Trying to get some basic examples of using an API with async
Resources
- Real Python tutorial - https://realpython.com/async-io-python/
Install
Download source code
git clone https://github.com/DavidTWynn/async_http_test
cd async_http_test
Create virtual environment
py -3.11 -m venv venv
venv/Scripts/activate
Upgrade pip
py -m pip install --upgrade pip
Use pip to install the project and dependencies
pip install -e .
Optional: Install dev dependencies to contribute
pip install -e .[dev]
Settings
The postgres database is setup with Docker compose. The postgres settings are configured with Docker secrets. These files will need to be created with the value stored for the setting. Create them at the top of the project level.
- postgres_db.txt
- postgres_password.txt
- postgres-user.txt
Files
- async_http_test/
- async_http_test.py - Examples for http async requests
- counter_async.py - sleep examples of sync and async
- Dockerfile - Settings to create a Docker image for the project
- .vscode/settings.json - MS VSCode IDE workspace settings for linting, formatting, etc. tests/ - pytest unit tests
- .gitignore - Files to not track by git
- .pre-commit-config.yaml - pre-commit hooks for git. Runs linting before committing
- google_docstring.mustache - Python autodocstring extension template based on Google Python style guide
- pyproject.toml - Project info, dependencies, and dev dependencies.
- README.md - Overview of the project
- .git - git change history files
- venv - Created on demand for project virtual environment for Python dependencies
Run
counter_async.py
Shows how you can use async with sleep vs a sync version
python async_http_test/counter_async.py
Example output
One
One
One
Two
Two
Two
Async executed in 1.00 seconds.
One
Two
One
Two
One
Two
Sync executed in 3.00 seconds.
pre-commit hooks
Linting to be ran before committing
- black
- isort
- flake8
- bandit
Update pre-commit hooks
If a hook is added to .pre-commit-config.yaml, update the .git/hooks files
pre-commit install
Create Docker image
Using the Dockerfile, a new Docker image can be created locally. This needs to be done in the project directory.
Make sure project is up to date
git pull
Create image
docker build -t async_http_test .
Verify image
docker image ls
Run image
docker run -it -d --name async_http_test async_http_test
Verify running container
docker container ls -a
Enter container
docker exec -it async_http_test bash
Run 'exit' from shell when done.
For development, VSCode's "Dev Containers" extension can be used to remote into the container and open the project in the /app directory. The code can be ran normally in the container. When VSCode is connected to the container, the VSCode extensions will then need to be installed for development.
Clean up Docker
When development work is done with Docker, the container can be stopped and the image can be deleted.
Find container name
docker container ls
Name should be under NAMES column for "async_http_test" using that IMAGE
Stop the container
docker container stop async_http_test
Delete the container (Needs to be stopped first)
docker container rm async_http_test
Locate the image (should be 'async_http_test')
docker image ls
Delete the image
docker image rm async_http_test