Skip to main content

Parallelize Python codes

Continuous integration badge License

parhugin provides functions to:

Table of contents

Installation

  1. install using pip

    pip install git+https://github.com/kasra-hosseini/parhugin.git
    
  2. install parhugin from the source code:

    • Clone parhugin source code:
    git clone https://github.com/kasra-hosseini/parhugin.git 
    
    • Install parhugin:
    cd /path/to/my/parhugin
    python setup.py install
    

    Alternatively:

    cd /path/to/my/parhugin
    pip install -v -e .
    

Run one or more Python functions in parallel using multiprocessing

In this scenario, we have:

  • one or more functions
  • a list of jobs to be run in parallel, e.g.:
[   
    [func1, (arg1_1, arg2_1, arg3_1)],
    [func1, (arg1_2, arg2_2, arg3_2)],  
    [func2, (...)],
    ...
] 

⚠️ If a function has only one argument, do not forget to add it to the above list either [func_one_arg, [arg1]] or [func_one_arg, (arg1,)].

  • User specifies the number of processes to be run in parallel.
  • parhugin parallelizes by distributing the jobs following FIFO on the requested number of processes.

Example 1

First, we import parhugin and define two simple functions called func1 and func2. These functions can have different number of arguments.

from parhugin import multiFunc
import time

# Define two simple functions, func1 and func2 
# Note that functions can have different number of arguments
def func1(a, b, sleep=0.5, info="func1"): 
    print(f"start, {info} calculated {a+b}")
    time.sleep(sleep)
    print(f"end, {info}")

def func2(a, sleep=0.2, info="func2"): 
    print(f"start, {info} prints {a}")
    time.sleep(sleep)
    print(f"end, {info}")

Next, we specify the number of processes to run in parallel. This can be the number of processors if the jobs are CPU-intensive. Otherwise, you can set this to any other values.

myproc = multiFunc(num_req_p=10)

Now, we need to add jobs to be run in parallel. There are different ways to do this:

  1. Add one function and its arguments:
myproc.add_job(target_func=func1, target_args=(2, 3, 0.5, "func1"))
print(myproc)

Similarly, we can add another function:

myproc.add_job(target_func=func2, target_args=(10, 0.2, "func2"))
print(myproc)
  1. Create a list of jobs:
list_jobs = []
for i in range(1, 20):
    list_jobs.append([func2, (f"{i}", 0.2, "func2")])

# and then adding them to myproc
myproc.add_list_jobs(list_jobs)
print(myproc)

Finally, run the jobs on the requested number of processes:

myproc.run_jobs()

It is also possible to change the verbosity level of the output by:

myproc.run_jobs(verbosity=2)

Complete examples

Example 1

from parhugin import multiFunc
import time

# Define two simple functions, func1 and func2 
# Note that functions can have different number of arguments
def func1(a, b, sleep=0.5, info="func1"): 
    print(f"start, {info} calculated {a+b}")
    time.sleep(sleep)
    print(f"end, {info}")

def func2(a, sleep=0.2, info="func2"): 
    print(f"start, {info} prints {a}")
    time.sleep(sleep)
    print(f"end, {info}")

myproc = multiFunc(num_req_p=10)
myproc.add_job(target_func=func1, target_args=(2, 3, 0.5, "func1"))
print(myproc)

myproc.add_job(target_func=func2, target_args=(10, 0.2, "func2"))
print(myproc)

list_jobs = []
for i in range(1, 20):
    list_jobs.append([func2, (f"{i}", 0.2, "func2")])

# and then adding them to myproc
myproc.add_list_jobs(list_jobs)
print(myproc)

myproc.run_jobs(verbosity=2)

Metadata

Release files for parhugin 0.0.3

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

Source distribution (sdist)

Source distribution for parhugin 0.0.3
File Size Uploaded
parhugin-0.0.3.tar.gz 6.3 kB Details

Release files / parhugin-0.0.3.tar.gz

Download URL parhugin-0.0.3.tar.gz
Size 6.3 kB
Tags Source
SHA-256 checksum
How to use checksums
edf0e987ceb65826f2ce11b11d9e4aac1a128e6ce2efea48d428ac4202720fcd
BLAKE2b-256 checksum
How to use checksums
1b66bf327e1b60a4fdf874f5365e28e71f803a4ea00311b956b15472e3d5d3fd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/42.0.2.post20191203 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.1

Release history Release notifications | RSS feed

This release

0.0.3 This release

1 release file

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