vmalloc / waiting

Python utility library for waiting for predicates
Other
50 stars 13 forks source link

Is there a way to log progress? #5

Closed erip closed 8 years ago

erip commented 8 years ago

This is a fantastic package and thanks so much for making it available!

Is there a way to log progress? For instance, I'd love to give the status of a predicate.

I'm using this to way for a Celery group to finish and I would find it incredibly useful to do something like this following:

wait(lambda: res.ready(),
     waiting_for="tasks ({0}) to finish".format(','.join(task.name for task in tasks)),
     logger=logger,
     info="{0} tasks finished".format(res.completed_count()),
     timeout_seconds=timeout)

so it logs the info every sleep_seconds seconds.

Is there any way to do this currently? I didn't see any examples that covered this.

vmalloc commented 8 years ago

You can do it with iterwait, but that would require a bit of coding.

For what you want specifically, perhaps pact will be more suitable? Have a look here: http://pact.readthedocs.io/en/stable/

erip commented 8 years ago

I'm not sure! I'd be happy to try to tinker with this to add an update_msg kwarg to wait. If I do, would you like me to submit a PR? I think it's a fairly trivial addition. :) Default value would be a simple noop.

Perhaps something like this:

wait(lambda: res.ready(),
     waiting_for="tasks ({0}) to finish".format(','.join(task.name for task in tasks)),
     update_msg=lambda: logger.info("{0} tasks finished".format(res.completed_count()),
     timeout_seconds=timeout)
vmalloc commented 8 years ago

@erip thanks for the PR! As I noted on the PR itself, I think poll_func or on_poll is a better name for such an argument

erip commented 8 years ago

@vmalloc Indeed. I've made the appropriate changes.

For safety, however, I've limited on_poll to be a lambda. Otherwise, someone could easily make their function:

def foo(): 
    print("Goodbye, world...")
    raise Exception

If you think this is too restrictive, I'd be happy to change that, too.

vmalloc commented 8 years ago

@erip I think it's too restrictive, and not very Pythonic. It's the user's responsibility to use callbacks judiciously...

Nobody is preventing you from writing:

on_poll=lambda: os.system("rm -rf /")

either, but people figure out the sane usage soon enough...

erip commented 8 years ago

Done. 👍