run_tasks(dataman, task, items, gui=True, poolsize=0, initargs=None, initkwds=None, verbose=None, numitems=None)¶
Run a series of tasks using multiple CPUs on a single computer.
Initialised with arguments:
DataManagerobject used to store the results in, see below.
- The task function or class (see below).
- A sequence (e.g. list or iterator) of arguments to be passed to the task.
- Whether or not to use a Tkinter based GUI to show progress and terminate the task run.
- The number of CPUs to use. If the value is 0, use all available CPUs, if it is -1 use all but one CPU, etc.
taskis a class, these are the initialisation arguments and keywords for the class.
- Specify True or False to print out every progress message (defaults to False if the GUI is used, or True if not).
- For iterables (rather than fixed length sequences), if you specify the number of items, an estimate of the time remaining will be given.
The task (defined by a function or class, see below) will be called on each item in
items, and the results saved to
dataman. Results are stored in the format
keyis a unique but meaningless identifier. Results can be retrieved using
dataman.values()or (for large data sets that should be iterated over)
The task can either be a function or a class. If it is a function, it will be called for each item in
items. If the items are tuples, the function will be called with those tuples as arguments (e.g. if the item is
(1,2,3)the function will be called as
task(1, 2, 3)). If the task is a class, it can have an
__init__method that is called once for each process (each CPU) at the beginning of the task run. If the
__init__method has a
process_numberargument, it will be passed an integer value from 0 to
numprocesses-1giving the number of the process (note, this is not the process ID). The class should define a
__call__method that behaves the same as above for
taskbeing a function. In both cases (function or class), if the arguments include a keyword
reportthen it will be passed a value that can be passed as the
reportkeyword in Brian’s
run()function to give feedback on the simulation as it runs. A
taskfunction can also set
self.tasknameas a string that will be displayed on the GUI to give additional information.
On Windows, make sure that
run_tasks()is only called from within a block such as:
if __name__=='__main__': run_tasks(...)
Otherwise, the program will go into a recursive loop.
Note that this class only allows you to run tasks on a single computer, to distribute work over multiple computers, we suggest using Playdoh.