Skip to main content

Create async web requests in no time

Project description

🔀 Unparallel

Create async HTTP requests with Python in no time.

Build status Coverage Report
Package Version Python Version
Ruff Security: bandit Pre-commit License
Docs Built with Material for MkDocs

With Unparallel you can easily create thousands of web requests in an efficient way leveraging Python's async capabilities.

Unparallel is built on top of HTTPX and aims to support its rich set of features.

Installation

pip install unparallel

Example

A simple example of doing several GET requests to an HTTP web service:

import asyncio

from unparallel import up


async def main():
    urls = [f"https://httpbin.org/get?i={i}" for i in range(5)]
    results = await up(urls)
    print([item["args"] for item in results])


if __name__ == "__main__":
    asyncio.run(main())

This prints:

Making async requests: 100%|███████████| 5/5 [00:00<00:00,  9.98it/s]
[{'i': '0'}, {'i': '1'}, {'i': '2'}, {'i': '3'}, {'i': '4'}]

Similarly, we can do a bunch of POST requests. This time we will use a single path but multiple payloads:

import asyncio

from unparallel import up


async def main():
    url = "https://httpbin.org/post"
    payloads = [{"obj_id": i} for i in range(5)]
    results = await up(url, method="post", payloads=payloads)
    print([item["data"] for item in results])


asyncio.run(main())

This prints:

Making async requests: 100%|███████████| 5/5 [00:00<00:00,  9.98it/s]
['{"obj_id": 0}', '{"obj_id": 1}', '{"obj_id": 2}', '{"obj_id": 3}', '{"obj_id": 4}']

For more details on the usage and examples, check out the docs.

Why unparallel? Why async?

Async is a really powerful feature - especially when you have to wait for I/O. When we create HTTP requests synchronously we have to wait for every response before we can start with the next request. If we utilize asynchronous programming, our runtime thread can do other work (other requests) during those periods of waiting.

Here is an example of making 20 web requests synchronously vs. asynchronously via unparallel.

Sync-vs-Async

As you can see, the async version finishes in less than a second while the sync code runs for around 10 seconds. The difference gets even more drastic if you create much more requests.

You can find the sync/async code in the docs folder.

Contributing

As this project is still in early development, I'm happy for any feedback and contributions! Please refer to the contributing guidelines for details.

License

This project is licensed under the terms of the MIT license. See LICENSE for more details.

Credits

This project was heavily inspired by the blog post Making 1 million requests with python-aiohttp by Paweł Miech.

I created this project with python-package-template.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

unparallel-0.4.0.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

unparallel-0.4.0-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file unparallel-0.4.0.tar.gz.

File metadata

  • Download URL: unparallel-0.4.0.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for unparallel-0.4.0.tar.gz
Algorithm Hash digest
SHA256 92a7aba2f66ab05445ecc6fd6fbc353de8de2f6698b51467f9a32277a718bb76
MD5 7d3a1f07b8c3dfae3771544b6571b102
BLAKE2b-256 aa3d3a2b63e1aa9e5a0fa88abd942b6ded9f8dfcb28729e19da2fda2645698de

See more details on using hashes here.

File details

Details for the file unparallel-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: unparallel-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for unparallel-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 866b9f76a3fef72c7826925700c6743320d58d00785a10bb6a06ead4e9bc689d
MD5 4c5fef7a49856ec48308814c10c9364b
BLAKE2b-256 f2a469978b5b5af930e81e82fef0b40a2d97b91df4af874582f697d6944852e1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page