1
2
3
4
5 """Code to schedule and run multiple processes.
6 """
7
8 import warnings
9 warnings.warn("Bio.MultiProc is deprecated. If you want to use this code, please let the Biopython developers know by sending an email to biopython-dev@biopython.org to avoid permanent removal of Bio.MultiProc.",
10 DeprecationWarning)
11
12 -def run(nprocs, fn, fn_args=(), fn_keywds={},
13 sleep=0.1, always_use_scheduler=0):
14 """run(nprocs, fn[, fn_args][, fn_keywds][, sleep])
15
16 Run nprocs copies of fn concurrently. The first two parameters of
17 fn should be a process number (0 to nprocs-1) and the total number
18 of processes. Additional arguments and keywords can be passed in
19 the fn_args and fn_keywds parameters. Sleep is the amount of time
20 the scheduler should wait between process polls.
21
22 """
23 if nprocs < 1 or nprocs > 100:
24 raise ValueError, "nprocs %d out of range" % nprocs
25
26
27
28
29 retvals = [None] * nprocs
30
31
32
33 if nprocs == 1 and not always_use_scheduler:
34 args = (0, 1) + fn_args
35 r = fn(*args, **fn_keywds)
36 retvals[0] = r
37 else:
38 import time
39 import Scheduler
40 import Task
41 def save_retval(task, retvals=retvals):
42 i = int(task.getName())
43 retvals[i] = task.retval
44 scheduler = Scheduler.Scheduler(nprocs, finish_fn=save_retval)
45 for i in range(nprocs):
46 args = (i, nprocs) + fn_args
47 task = Task.Task(name=i, target=fn, args=args, kwargs=fn_keywds)
48 scheduler.add(task)
49 while scheduler.run():
50 time.sleep(sleep)
51 return retvals
52