Back to index

python-biopython  1.60
Local.py
Go to the documentation of this file.
00001 # Copyright 2007 by Tiago Antao <tiagoantao@gmail.com>.  All rights reserved.
00002 # This code is part of the Biopython distribution and governed by its
00003 # license.  Please see the LICENSE file that should have been included
00004 # as part of this package.
00005 
00006 '''
00007 Asynchronous local execution.
00008 
00009 Supports multicore architectures.
00010 '''
00011 
00012 from Bio.PopGen.Async import Async, FileRetriever
00013 
00014 import thread
00015 
00016 class Local(Async):
00017     '''Execution on Local machine.
00018     '''
00019 
00020     def __init__(self, num_cores = 1):
00021         '''Constructor.
00022 
00023            parameters:
00024            num_cores - Number of cores (for multiprocessor machines,
00025                multiply accordingly)
00026         '''
00027         Async.__init__(self)
00028         self.num_cores = num_cores
00029         self.cores_used = 0
00030 
00031     def _run_program(self, id, hook, parameters, input_files):
00032         '''Run program.
00033 
00034            For parameters, please check Async.run_program.
00035 
00036            Either runs a program if a core is available or
00037            schedules it.
00038         '''
00039         self.access_ds.acquire()
00040         self.waiting.append((id, hook, parameters, input_files))
00041         if self.cores_used < self.num_cores:
00042             self.cores_used += 1
00043             thread.start_new_thread(self.start_work, ())
00044         self.access_ds.release()
00045 
00046     def start_work(self):
00047         '''Starts work.
00048 
00049            Thread initial point.
00050            While there are tasks to be done, runs them.
00051            The thread dies as soon as there is nothing waiting to be
00052            executed.
00053         '''
00054         self.access_ds.acquire()
00055         while (len(self.waiting) > 0):
00056             id, hook, parameters, input_files = self.waiting[0]
00057             del self.waiting[0]
00058             self.running[id] = True
00059             self.access_ds.release()
00060             ret_code, output_files = hook.run_job(parameters, input_files)
00061             self.access_ds.acquire()
00062             del self.running[id]
00063             self.done[id] = ret_code, output_files
00064         self.cores_used -= 1
00065         self.access_ds.release()
00066