A package to mimic the use of parfor as done in Matlab.
Project description
Parfor
Used to parallelize for-loops using parfor in Matlab? This package allows you to do the same in python. Take any normal serial but parallelizable for-loop and execute it in parallel using easy syntax. Don't worry about the technical details of using the multiprocessing module, race conditions, queues, parfor handles all that. Now powered by ray.
Tested on linux, Windows and OSX with python 3.10 and 3.12.
Why is parfor better than just using multiprocessing?
- Easy to use
- Progress bars are built-in
- Retry the task in the main process upon failure for easy debugging
- Using a modified version of dill when ray fails to serialize an object: a lot more objects can be used when parallelizing
How it works
Ray does all the heavy lifting. Parfor now is just a wrapper around ray, adding some ergonomics.
Installation
pip install parfor
Usage
Parfor decorates a functions and returns the result of that function evaluated in parallel for each iteration of an iterator.
Requires
numpy, ray, tqdm
Arguments
To functions parfor.parfor, parfor.pmap and parfor.gmap.
Required:
fun: function taking arguments: iteration from iterable, other arguments defined in args & kwargs
iterable: iterable or iterator from which an item is given to fun as a first argument
Optional:
args: tuple with other unnamed arguments to fun
kwargs: dict with other named arguments to fun
total: give the length of the iterator in cases where len(iterator) results in an error
desc: string with description of the progress bar
bar: bool enable progress bar,
or a callback function taking the number of passed iterations as an argument
serial: execute in series instead of parallel if True, None (default): let pmap decide
n_processes: number of processes to use,
the parallel pool will be restarted if the current pool does not have the right number of processes
yield_ordered: return the result in the same order as the iterable
yield_index: return the index of the result too
allow_output: allow output from subprocesses
**bar_kwargs: keyword arguments for tqdm.tqdm
Return
list with results from applying the function 'fun' to each iteration of the iterable / iterator
Examples
Normal serial for loop
<<
from time import sleep
a = 3
fun = []
for i in range(10):
sleep(1)
fun.append(a * i ** 2)
print(fun)
>> [0, 3, 12, 27, 48, 75, 108, 147, 192, 243]
Using parfor to parallelize
<<
from time import sleep
from parfor import parfor
@parfor(range(10), (3,))
def fun(i, a):
sleep(1)
return a * i ** 2
print(fun)
>> [0, 3, 12, 27, 48, 75, 108, 147, 192, 243]
<<
@parfor(range(10), (3,), bar=False)
def fun(i, a):
sleep(1)
return a * i ** 2
print(fun)
>> [0, 3, 12, 27, 48, 75, 108, 147, 192, 243]
Using parfor in a script/module/.py-file
Parfor should never be executed during the import phase of a .py-file. To prevent that from happening
use the if __name__ == '__main__': structure:
<<
from time import sleep
from parfor import parfor
if __name__ == '__main__':
@parfor(range(10), (3,))
def fun(i, a):
sleep(1)
return a * i ** 2
print(fun)
>> [0, 3, 12, 27, 48, 75, 108, 147, 192, 243]
or:
<<
from time import sleep
from parfor import parfor
def my_fun(*args, **kwargs):
@parfor(range(10), (3,))
def fun(i, a):
sleep(1)
return a * i ** 2
return fun
if __name__ == '__main__':
print(my_fun())
>> [0, 3, 12, 27, 48, 75, 108, 147, 192, 243]
If you hate decorators not returning a function
pmap maps an iterator to a function like map does, but in parallel
<<
from parfor import pmap
from time import sleep
def fun(i, a):
sleep(1)
return a * i ** 2
print(pmap(fun, range(10), (3,)))
>> [0, 3, 12, 27, 48, 75, 108, 147, 192, 243]
Using generators
If iterators like lists and tuples are too big for the memory, use generators instead. Since generators don't have a predefined length, give parfor the length (total) as an argument (optional).
<<
import numpy as np
c = (im for im in imagereader)
@parfor(c, total=len(imagereader))
def fun(im):
return np.mean(im)
>> [list with means of the images]
Extra's
pmap
The function parfor decorates, it's used similarly to map, it returns a list with the results.
gmap
Same as pmap, but returns a generator. Useful to use the result as soon as it's generated.
Chunks
Split a long iterator in bite-sized chunks to parallelize
ParPool
More low-level accessibility to parallel execution. Submit tasks and request the result at any time, (although to avoid breaking causality, submit first, then request), use different functions and function arguments for different tasks.
SharedArray
A numpy arrow that can be shared among processes.
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 parfor-2026.2.2.tar.gz.
File metadata
- Download URL: parfor-2026.2.2.tar.gz
- Upload date:
- Size: 13.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5832e962e700ac4a1bfe6a8185481b451286f551cdcdb819b0bb5f7c9e880cda
|
|
| MD5 |
5f5e91c03aa2e8feea604ef5080bcf95
|
|
| BLAKE2b-256 |
ae8cd50b10f1fb3cbf7b232ad29695aec74f2091c8e600a85e0a24067ed36648
|
Provenance
The following attestation bundles were made for parfor-2026.2.2.tar.gz:
Publisher:
publish.yml on wimpomp/parfor
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
parfor-2026.2.2.tar.gz -
Subject digest:
5832e962e700ac4a1bfe6a8185481b451286f551cdcdb819b0bb5f7c9e880cda - Sigstore transparency entry: 845015871
- Sigstore integration time:
-
Permalink:
wimpomp/parfor@8619c0fb34371d9c909c1ab7df03c6e84729b7f6 -
Branch / Tag:
refs/heads/master - Owner: https://github.com/wimpomp
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@8619c0fb34371d9c909c1ab7df03c6e84729b7f6 -
Trigger Event:
push
-
Statement type:
File details
Details for the file parfor-2026.2.2-py3-none-any.whl.
File metadata
- Download URL: parfor-2026.2.2-py3-none-any.whl
- Upload date:
- Size: 11.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5cca78728c4a184c4ec9bb97571faec77f2bd10dfcdb9b07d35fec136ee62931
|
|
| MD5 |
c4b68ab7c52aba143a7c007bee4c058d
|
|
| BLAKE2b-256 |
6c9b110b6728ed54400c9ccb7ba28b5f818491ba75c9fc4c9f87684cbee5fc62
|
Provenance
The following attestation bundles were made for parfor-2026.2.2-py3-none-any.whl:
Publisher:
publish.yml on wimpomp/parfor
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
parfor-2026.2.2-py3-none-any.whl -
Subject digest:
5cca78728c4a184c4ec9bb97571faec77f2bd10dfcdb9b07d35fec136ee62931 - Sigstore transparency entry: 845015878
- Sigstore integration time:
-
Permalink:
wimpomp/parfor@8619c0fb34371d9c909c1ab7df03c6e84729b7f6 -
Branch / Tag:
refs/heads/master - Owner: https://github.com/wimpomp
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@8619c0fb34371d9c909c1ab7df03c6e84729b7f6 -
Trigger Event:
push
-
Statement type: