# Module Scientific.Threading.TaskManager

## Class TaskManager: Parallel task manager for shared-memory multiprocessor machines

This class provides a rather simple way to profit from shared-memory multiprocessor machines by running several tasks in parallel. The calling program decides how many execution threads should run at any given time, and then feeds compute tasks to the task manager, who runs them as soon as possible without exceeding the maximum number of threads.

The major limitation of this approach lies in Python's Global Interpreter Lock. This effectively means that no more than one Python thread can run at the same time. Consequently, parallelization can only be achieved if the tasks to be parallelized spend significant time in C extension modules that release the Global Interpreter Lock.

Constructor: TaskManager(nthreads)

nthreads

  the maximum number of compute threads that should run in parallel. Note: This does not include the main thread which generated and feeds the task manager!

Methods:

- runTask(function, args)

  Add a task defined by function. This must be a callable object, which will be called exactly once. The arguments of the call are the elements of the tuple args plus one additional argument which is a lock object. The task can use this lock object in order to get temporary exclusive acces to data shared with other tasks, e.g. a list in which to accumulate results.

- terminate()

  Wait until all tasks have finished.