Skip to main content

hpylib

hpylib is a small Python library that provides simple building blocks for parallel programming with threads. It is designed to make it easy to run tasks asynchronously, coordinate them, and perform parallel map operations, all with minimal boilerplate.

It provides three main functions:

  1. spawn(fn) — run a function asynchronously in a worker thread.
  2. finish() — context manager to wait for all asynchronous tasks submitted inside its block.
  3. pmap(fn, iterable) — parallel map: apply a function to each element of an iterable in parallel, returning the results as a list.

Installation

pip install hpylib

Usage

1. Asynchronous Tasks with spawn and finish

from hpylib import spawn, finish
import time

def work(x):
    time.sleep(1)
    print(f"Processed {x}")

with finish():
    for i in range(5):
        spawn(lambda i=i: work(i))

print("All tasks completed!")
  • spawn(fn) submits a task to run in the background.

  • finish() waits for all tasks submitted inside its block to complete.

2. Parallel Map with pmap

from hpylib import pmap
from math import isqrt

def is_prime(n):
    if n < 2:
        return False
    for i in range(2, isqrt(n)+1):
        if n % i == 0:
            return False
    return True

numbers = range(1, 100_000)
results = pmap(is_prime, numbers)
total_primes = sum(results)
print(f"Number of primes: {total_primes}")
  • pmap(fn, iterable) automatically splits the iterable into chunks and distributes the work across the thread pool.

  • Returns a list of results in the same order as the input.

  • Blocks until all tasks are complete.

Metadata

Release files for hpylib 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hpylib 0.1.0
File Size Uploaded
hpylib-0.1.0.tar.gz 3.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for hpylib 0.1.0
File Interpreter ABI Platform
hpylib-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 7.2 kB

Release files / hpylib-0.1.0.tar.gz

Download URL hpylib-0.1.0.tar.gz
Size 3.5 kB
Tags Source
SHA-256 checksum
How to use checksums
68ff55ea3083ef0b915345a3965773e63cab234d057e05d2d4c5069826a4a0e7
BLAKE2b-256 checksum
How to use checksums
f159f7597813d52a63c7e44cfb00000bbac21760ad97b7c396a55e68348b9d03
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.2

Release files / hpylib-0.1.0-py3-none-any.whl

Download URL hpylib-0.1.0-py3-none-any.whl
Size 3.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5d8147631f8b4e2be614cb25319849a42aed64e3215a8f08cf2139e60bd9b90b
BLAKE2b-256 checksum
How to use checksums
cc35b904216674d121d7737940085ac19e28bf1bad134d14588eae24f438ec29
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.2

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page