A small Python library for simple parallel programming (spawn, finish, pmap).
Project description
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:
spawn(fn)— run a function asynchronously in a worker thread.finish()— context manager to wait for all asynchronous tasks submitted inside its block.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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file hpylib-0.1.0.tar.gz.
File metadata
- Download URL: hpylib-0.1.0.tar.gz
- Upload date:
- Size: 3.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
68ff55ea3083ef0b915345a3965773e63cab234d057e05d2d4c5069826a4a0e7
|
|
| MD5 |
77c949668b390213c912973612fb87aa
|
|
| BLAKE2b-256 |
f159f7597813d52a63c7e44cfb00000bbac21760ad97b7c396a55e68348b9d03
|
File details
Details for the file hpylib-0.1.0-py3-none-any.whl.
File metadata
- Download URL: hpylib-0.1.0-py3-none-any.whl
- Upload date:
- Size: 3.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5d8147631f8b4e2be614cb25319849a42aed64e3215a8f08cf2139e60bd9b90b
|
|
| MD5 |
b24b438d4576a8d8503b2b8c7eb9c256
|
|
| BLAKE2b-256 |
cc35b904216674d121d7737940085ac19e28bf1bad134d14588eae24f438ec29
|