Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'callback' feature, with some other changes #1

Closed
wants to merge 2 commits into from
Closed

Add 'callback' feature, with some other changes #1

wants to merge 2 commits into from

Conversation

derks
Copy link

@derks derks commented May 20, 2011

Currently there is no way to handle erros should a command not run successfully. I've added the use of subprocess.PIPE, and a callback feature to Pool.run(). Here's an an example use case:

from littleworkers import Pool

def my_callback(proc):
    print "Command exited with code %s" % proc.returncode
    if proc.returncode != 0:
        print "STDERR: %s" % proc.stderr.read()
    # do other actions here


cmds = [
    'ls -lah',
    'bogus-command-doesnt-exist',
    ]

lil = Pool(workers=2)
lil.run(cmds, callback=my_callback)

And looks like:

(littleworkers)derks-macbook [littleworkers] $ python test.py 
Command exited with code 0
Command exited with code 127
STDERR: /bin/sh: bogus-command-doesnt-exist: command not found

There are some other changes in there... such as adding the 'wait_time' option to Pool() as there was previously no way to alter that. I've also refactored how commands are processed by replacing 'next_command()' with an iterator object allowing the 'run()' loop to simply call 'process_commands.next()' rather than popping commands off a list and what not.

Please let me know if you have any questions about any of that.

BJ Dierkes added 2 commits May 19, 2011 21:41
 - Add ability to pass a callback function to run()
 - Also refactored how commands are processed using a generator
@derks
Copy link
Author

derks commented May 20, 2011

Just noticed in the readme how you reference subclassing in order to get stdout, etc. I can see why you might not want to create a PIPE for all processes on stdout/stderr (for example.... something that outputs gigs of text). Maybe could make that a bit more configurable.... but, I think processing gigs of output would probably be outside the scope of this project at this point.

@toastdriven
Copy link
Owner

Some good ideas here. I'm out of time I can spend on littleworkers tonight, but I'll revisit this tomorrow night & merge in portions.

@toastdriven
Copy link
Owner

I cherry-picked out the bits I liked (which was pretty much everything). Fixed in SHA: 3f6f92f, SHA: a3a9ec6 & SHA: da66841.

I didn't pick out the iterator bits, because IMO it's easier for the rest of the class to reason about how much work is left if inspecting self.commands is all it has to do. If there's an iterator, then you have to store the iterator on the class for other parts (outside the run method) to be able to determine status, which doesn't thrill me.

And it's funny that you mention gigs of text, because in my first production usage, that's actually possible. :) I felt like user-configurability was the best solution there.

Thanks for the patience!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants