Rename trunk from gaworkflow to htsworkflow (and update all of the imports)
[htsworkflow.git] / htsworkflow / util / queuecommands.py
1 """
2 Run up to N simultanous jobs from provided of commands 
3 """
4
5 import logging
6 from subprocess import PIPE
7 import subprocess
8 import select
9 import sys
10 import time
11
12 class QueueCommands(object):
13     """
14     Queue up N commands from cmd_list, launching more jobs as the first
15     finish.
16     """
17
18     def __init__(self, cmd_list, N=0, cwd=None):
19         """
20         cmd_list is a list of elements suitable for subprocess
21         N is the  number of simultanious processes to run. 
22         0 is all of them.
23         
24         WARNING: this will not work on windows
25         (It depends on being able to pass local file descriptors to the 
26         select call with isn't supported by the Win32 API)
27         """
28         self.to_run = cmd_list[:]
29         self.running = {}
30         self.N = N
31         self.cwd = cwd
32
33     def under_process_limit(self):
34         """
35         are we still under the total number of allowable jobs?
36         """
37         if self.N == 0:
38             return True
39
40         if len(self.running) < self.N:
41             return True
42
43         return False
44
45     def start_jobs(self):
46         """
47         Launch jobs until we have the maximum allowable running
48         (or have run out of jobs)
49         """
50         queue_log = logging.getLogger('queue')
51         queue_log.info('using %s as cwd' % (self.cwd,))
52
53         while (len(self.to_run) > 0) and self.under_process_limit():
54             queue_log.info('%d left to run', len(self.to_run))
55             cmd = self.to_run.pop(0)
56             p = subprocess.Popen(cmd, stdout=PIPE, cwd=self.cwd, shell=True)
57             self.running[p.stdout] = p
58             queue_log.info("Created process %d from %s" % (p.pid, str(cmd)))
59
60     def run(self):
61         """
62         run up to N jobs until we run out of jobs
63         """
64         queue_log = logging.getLogger('queue')
65
66         # to_run slowly gets consumed by start_jobs
67         while len(self.to_run) > 0 or len(self.running) > 0:
68             # fill any empty spots in our job queue
69             self.start_jobs()
70
71             # build a list of file descriptors
72             # fds=file desciptors
73             fds = [ x.stdout for x in self.running.values()]
74
75             # wait for something to finish
76             # wl= write list, xl=exception list (not used so get bad names)
77             read_list, wl, xl = select.select(fds, [], fds)
78         
79             # for everything that might have finished...
80             for pending_fd in read_list:
81                 pending = self.running[pending_fd]
82                 # if it really did finish, remove it from running jobs
83                 if pending.poll() is not None:
84                     queue_log.info("Process %d finished [%d]",
85                                    pending.pid, pending.returncode)
86                     del self.running[pending_fd]
87             time.sleep(1)