API¶
-
class
littleworkers.
Pool
(workers=1, debug=False, wait_time=0.1)¶ 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.-
add_to_pool
(proc)¶ Adds a process to the pool.
-
busy_wait
()¶ A hook to control how often the busy-wait loop runs.
By default, sleeps for 0.1 seconds.
-
command_count
()¶ Returns the number of commands to be run.
Useful as a hook if you use a different structure for the commands.
-
create_process
(command)¶ Given a provided command (string or list), creates a new process to execute the command.
-
inspect_pool
()¶ A hook for inspecting the pool’s current status.
By default, simply makes a log message and returns the length of the pool.
-
next_command
()¶ Fetches the next command for processing.
Will return
None
if there are no commands remaining (unlessPool.debug = True
).
-
prepare_commands
(commands)¶ A hook to override how the commands are added.
By default, simply copies the provided command
list
to the internalcommands
list.
-
process_kwargs
(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
.
-
remove_from_pool
(pid)¶ Removes a process to the pool.
Fails silently if the process id is no longer present (unless
Pool.debug = True
).
-
run
(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 callPool.prepare_commands
.
-
set_callback
(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.
-