Package Bio :: Package MultiProc
[hide private]
[frames] | no frames]

Source Code for Package Bio.MultiProc

 1  # Copyright 2001 by Jeffrey Chang.  All rights reserved. 
 2  # This code is part of the Biopython distribution and governed by its 
 3  # license.  Please see the LICENSE file that should have been included 
 4  # as part of this package. 
 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 # Undocumented: return a list of return values. The return values 26 # of the functions must be pickle-able types. This is 27 # undocumented and may go away in future releases. I'm not sure 28 # it's a good idea right now... 29 retvals = [None] * nprocs 30 31 # If they only want to run 1 process, then just run it without 32 # going through the scheduler. 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