Code Monkey home page Code Monkey logo

parallel-execute's Introduction

parallel-execute

Python wrappers for easy multiprocessing and threading.

Run multiple functions in parallel using parallel-execute

GitHub

Latest documentation

PyPI

PyPI - Wheel

PyPI - Python Version

Installation

pip install parallel-execute

Usage Example

1. Create a loom:

This takes a number of tasks and executes them using a pool of threads/process.

  • To use threading
from pexecute.thread import ThreadLoom
loom = ThreadLoom(max_runner_cap=10)
  • To use multiprocessing
from pexecute.process import ProcessLoom
loom = ProcessLoom(max_runner_cap=10)

max_runner_cap: is the number of maximum threads/processes to run at a time. You can add as many as functions you want, but only n functions will run at a time in parallel, n is the max_runner_cap

2. Add tasks in loom

  • Add a function in loom using add_function
loom.add_function(f1, args1, kw1)
loom.add_function(f2, args2, kw2)
loom.add_function(f3, args3, kw3)
  • Add multiple functions together using add_work method
work = [(f1, args1, kwargs1), (f2, args2, kwargs2), (f3, args3, kwargs3)]
loom.add_work(work)

3. Execute all tasks

After adding tasks, calling execute will return a dictionary of results corresponding to the keys or the order in which the tasks were added.

output = loom.execute()

key is the order in which the function was added and value is the return data of the function.

# Example:

def fun1():
   return "Hello World"

def fun2(a):
   return a

def fun3(a, b=0):
   return a+b

loom.add_function(fun1, [], {})
loom.add_function(fun2, [1], {})
loom.add_function(fun3, [1], {'b': 3})

output = loom.execute()
>>> output
    {
     0: {'output': 'Hello World',
         'got_error': False,
         'error': None,
         'started_time': datetime.datetime(2019, 6, 28, 19, 44, 58, 395002),
         'finished_time': datetime.datetime(2019, 6, 28, 19, 44, 58, 396500),
         'execution_time': 0.001498,
         },
     1: {'output': 1,
         'got_error': False,
         'error': None,
         'started_time': datetime.datetime(2019, 6, 28, 19, 44, 58, 396590),
         'finished_time': datetime.datetime(2019, 6, 28, 19, 44, 58, 397651),
         'execution_time': 0.001061
         },
     2: {'output': 4,
         'got_error': False,
         'error': None,
         'started_time': datetime.datetime(2019, 6, 28, 19, 44, 58, 400323),
         'finished_time': datetime.datetime(2019, 6, 28, 19, 44, 58, 401749),
         'execution_time': 0.001426
         }
    }

We can also provide a key to store the function return data.

# Example:
loom.add_function(fun1, [], {}, 'key1')
loom.add_function(fun2, [1], {}, 'fun2')
loom.add_function(fun3, [1], {'b': 3}, 'xyz')

output = loom.execute()
>>> output
    {
     'key1': {'output': 'Hello World',
             'got_error': False,
             'error': None,
             'started_time': datetime.datetime(2019, 6, 28, 19, 44, 58, 395002),
             'finished_time': datetime.datetime(2019, 6, 28, 19, 44, 58, 396500),
             'execution_time': 0.001498,
             },
     'fun2: {'output': 1,
             'got_error': False,
             'error': None,
             'started_time': datetime.datetime(2019, 6, 28, 19, 44, 58, 396590),
             'finished_time': datetime.datetime(2019, 6, 28, 19, 44, 58, 397651),
             'execution_time': 0.001061
             },
     'xyz': {'output': 4,
             'got_error': False,
             'error': None,
             'started_time': datetime.datetime(2019, 6, 28, 19, 44, 58, 400323),
             'finished_time': datetime.datetime(2019, 6, 28, 19, 44, 58, 401749),
             'execution_time': 0.001426
             }
    }

parallel-execute's People

Contributors

sahilrp avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.