Skip to main content

Decorator for async batch calling via http requests

Project description

batch-async-http

This tool was designed to make batch async requests via http very simple! All you need to do is attach a decorator to your http request function and the rest is handled for you.

Behind the scenes your input is turned into a generator with batches of your specified size, and mapped asynchronously to your http request function!

See the source for this project here: https://https://github.com/MiesnerJacob/batch_async_http.

How to install:

pip install batch-async-http

Usage

A typical function to make http requests to a REST API may look something like this:

import requests

def call_api(example_input: list):

        # URL to call
        url = 'http://xx.xxx.xxx.xx/example_endpoint/'

        # Input to API
        params = {
            "raw": example_input,
        }

        # API calls
        r = requests.post(url, json=params)
        response = r.json()
        
        return response

A typical async function to make http requests to a REST API may look something like this:

import httpx

async def call_api(example_input: list):

        # URL to call
        url = 'http://xx.xxx.xxx.xx/example_endpoint/'

        # Input to API
        params = {
            "raw": example_input,
        }

        # Concurrent API calls
        async with httpx.AsyncClient(timeout=None) as client:
            r = await client.post(url, json=params)
        response = r.json()
        
        return response

The power of batching your async requests is now just as easy as applying a simple decorator to your async function:

import httpx
from batch_async_http import batch_async

@batch_async(size=8)
async def call_api(example_input: list):

        # URL to call
        url = 'http://xx.xxx.xxx.xx/example_endpoint/'

        # Input to API
        params = {
            "raw": example_input,
        }

        # Concurrent API calls
        async with httpx.AsyncClient(timeout=None) as client:
            r = await client.post(url, json=params)
        response = r.json()
        
        return response

Enjoy your increased efficiency with minimal effort!

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

batch_async_http-0.0.1.tar.gz (2.9 kB view details)

Uploaded Source

File details

Details for the file batch_async_http-0.0.1.tar.gz.

File metadata

  • Download URL: batch_async_http-0.0.1.tar.gz
  • Upload date:
  • Size: 2.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/3.10.1 pkginfo/1.8.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.6

File hashes

Hashes for batch_async_http-0.0.1.tar.gz
Algorithm Hash digest
SHA256 e215fe45d6200d7767b7d6909fabc33866886d9d62d3966a215dd238a1aa3b61
MD5 7a400d971729fc03c29e04fd2274c1a4
BLAKE2b-256 6dccb862e3fb48e5ca5f899ccea95d59c4ca8b8001e2786860c3b4cf3ffcf280

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