forked from toastdriven/littleworkers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
littleworkers.py
197 lines (151 loc) · 5.24 KB
/
littleworkers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import logging
import os
import subprocess
import time
__author__ = 'Daniel Lindsley'
__version__ = (0, 3, 2)
__license__ = 'BSD'
class LittleWorkersException(Exception):
pass
class NotEnoughWorkers(LittleWorkersException):
pass
class Pool(object):
"""
The main pool object. Manages a set of specified workers.
Usage::
commands = [
'ls -al',
'cd /tmp && mkdir foo',
'date',
'echo "Hello There."',
'sleep 2 && echo "Done."'
]
lil = Pool(workers=2)
lil.run(commands)
Optionally accepts a ``workers`` kwarg. Default is 1.
Optionally accepts a ``debug`` kwarg. Default is False.
Optionally accepts a ``wait_time`` kwarg. Default is 0.1.
"""
def __init__(self, workers=1, debug=False, wait_time=0.1):
if workers < 1:
raise NotEnoughWorkers("You need to use at least one worker.")
self.workers = workers
self.pool = {}
self.commands = []
self.callback = None
self.debug = debug
self.wait_time = wait_time
def prepare_commands(self, commands):
"""
A hook to override how the commands are added.
By default, simply copies the provided command ``list`` to the
internal ``commands`` list.
"""
# Make a copy of the commands to run.
self.commands = commands[:]
def command_count(self):
"""
Returns the number of commands to be run.
Useful as a hook if you use a different structure for the commands.
"""
return len(self.commands)
def next_command(self):
"""
Fetches the next command for processing.
Will return ``None`` if there are no commands remaining (unless
``Pool.debug = True``).
"""
try:
return self.commands.pop(0)
except IndexError:
if self.debug:
raise
return None
def process_kwargs(self, command):
"""
A hook to alter the kwargs given to ``subprocess.Process``.
Takes a ``command`` argument, which is unused by default, but can be
used to switch the flags used.
By default, only specifies ``shell=True``.
"""
return {
'shell': True,
}
def create_process(self, command):
"""
Given a provided command (string or list), creates a new process
to execute the command.
"""
logging.debug("Starting process to handle command '%s'." % command)
kwargs = self.process_kwargs(command)
return subprocess.Popen(command, **kwargs)
def set_callback(self, callback=None):
"""
Sets up a callback to be run whenever a process finishes.
If called with ``None`` or without any args, it will clear any
existing callback.
"""
self.callback = callback
def add_to_pool(self, proc):
"""
Adds a process to the pool.
"""
logging.debug("Adding %s to the pool." % proc.pid)
self.pool[proc.pid] = proc
def remove_from_pool(self, pid):
"""
Removes a process to the pool.
Fails silently if the process id is no longer present (unless
``Pool.debug = True``).
"""
try:
logging.debug("Removing %s from the pool" % pid)
del(self.pool[pid])
except KeyError:
if self.debug:
raise
def inspect_pool(self):
"""
A hook for inspecting the pool's current status.
By default, simply makes a log message and returns the length of
the pool.
"""
# Call ``len()`` just once.
pool_size = len(self.pool)
logging.debug("Current pool size: %s" % pool_size)
return pool_size
def busy_wait(self):
"""
A hook to control how often the busy-wait loop runs.
By default, sleeps for 0.1 seconds.
"""
time.sleep(self.wait_time)
def run(self, commands=None, callback=None):
"""
The method to actually execute all the commands with the pool.
Optionally accepts a ``commands`` kwarg, as a shortcut not to have to
call ``Pool.prepare_commands``.
"""
if commands is not None:
self.prepare_commands(commands)
if callback is not None:
self.set_callback(callback)
keep_running = True
while keep_running:
self.inspect_pool()
if len(self.pool) <= min(self.command_count(), self.workers):
command = self.next_command()
if not command:
self.busy_wait()
continue
proc = self.create_process(command)
self.add_to_pool(proc)
# Go in reverse order so offsets never get screwed up.
for pid in self.pool.keys():
logging.debug("Checking status on %s" % self.pool[pid].pid)
if self.pool[pid].poll() >= 0:
if self.callback:
self.callback(self.pool[pid])
self.remove_from_pool(pid)
keep_running = self.command_count() or len(self.pool) > 0
self.busy_wait()