Skip to main content

Thread-based parallelism for Python. Simple, explicit API for running tasks in parallel.

Project description

PyAsync

Thread-based parallelism for Python. Simple, explicit API for running tasks in parallel.

No magic, just straightforward parallel execution using threads.

Installation

pip install python-async

Quick Start

import pyasync
import requests

def fetch(url):
    return requests.get(url).json()

# Run 3 requests in parallel - takes ~1 second, not ~3 seconds!
results = pyasync.parallel(
    lambda: fetch("https://api.example.com/users/1"),
    lambda: fetch("https://api.example.com/users/2"),
    lambda: fetch("https://api.example.com/users/3")
)

API

parallel(*callables)

Run multiple functions in parallel threads. Returns results in order.

results = pyasync.parallel(
    lambda: requests.get("https://api1.com"),
    lambda: requests.get("https://api2.com"),
    lambda: requests.get("https://api3.com")
)
# All 3 run simultaneously!

background(callable)

Start a function in the background. Returns a Task.

task = pyasync.background(lambda: slow_operation())

# Do other work while it runs...
print("Working...")

# Get result when ready
result = task.result()

run(callable)

Run a single function in the thread pool.

result = pyasync.run(lambda: requests.get("https://api.com"))

Examples

Parallel Tasks

$ python examples/simple_parallel.py

=== Simple Parallel Tasks ===

[Task A] Starting...
[Task B] Starting...
[Task C] Starting...
[Task A] Done!
[Task C] Done!
[Task B] Done!

Results: ['Task A completed', 'Task B completed', 'Task C completed']
Total time: 2.01s (longest task was 2s)

Parallel API Calls

$ python examples/parallel_api_calls.py

=== Parallel API Calls ===

Fetching 3 users in parallel...
  - Leanne Graham (Sincere@april.biz)
  - Ervin Howell (Shanna@melissa.tv)
  - Clementine Bauch (Nathan@yesenia.net)

Fetching posts for each user...
  - Leanne Graham: 10 posts
  - Ervin Howell: 10 posts
  - Clementine Bauch: 10 posts

Parallel File Processing

$ python examples/parallel_files.py

=== Parallel File Processing ===

Processing 2 files in parallel...

File                 Size       Hash      
----------------------------------------
__init__.py          587        8c137857  
runtime.py           3363       0bca3ba5

Web Scraping

$ python examples/web_scraping.py

Scraping 5 URLs in parallel...

Total time:                    0.69s
Sequential would take:         1.23s
Speedup:                       1.8x faster

When to Use

Good for:

  • Scripts making multiple HTTP requests
  • Batch processing I/O operations
  • Parallel file operations

Not for:

  • Thousands of concurrent connections (use asyncio)
  • CPU-bound tasks (use multiprocessing)

Testing

python -m unittest discover -s tests -v

License

MIT

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

python_async-0.2.0.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_async-0.2.0-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file python_async-0.2.0.tar.gz.

File metadata

  • Download URL: python_async-0.2.0.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for python_async-0.2.0.tar.gz
Algorithm Hash digest
SHA256 9821c6e499b07cdf9986771bc7ab566fa432f5a47c850dcbfdf7b305891bd011
MD5 a8aa4b2d2b1148b2daf5f26de320be66
BLAKE2b-256 2726243b425b2929625e72788915b5a22ac976a7cf779cbda5505be5e3217276

See more details on using hashes here.

File details

Details for the file python_async-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: python_async-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for python_async-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 115ba34114d261f6212b21e6463d2acee7b8ad9c7556ab8feb1cae4d4b0e974b
MD5 760159cace62b0d26c61e63d86be2d37
BLAKE2b-256 e0076fc913da1bcc18743ea9e4434d51f2d26c431c621a56ab7f13e312cb6a1d

See more details on using hashes here.

Supported by

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