Re: How to retry something with a timeout in Python?
Scott David Daniels scott.dani...@acm.org wrote: tinn...@isbd.co.uk wrote: This feels like it should be simple but I can't see a clean way of doing it at the moment. I want to retry locking a file for a number of times and then give up, in pseudo-code it would be something like:- for N times try to lock file if successful break out of for loop if we don't have a lock then give up and exit for attempt in range(N): try: lock_file_with_timeout(per_try) # change to what you mean except LockAttemptFailure: # or however the failure is shown pass # here the attempt+1th try failed. else: break # success -- have the lock else: raise ImTiredError # however you handle N attempts w/o success rest_of_code Ah, yes, it's the 'else:' with the 'for' that makes it easier, I've come from languages which don't have that, thank you! :-) Often it is easiest to stick it in a function: def retry_lock(tries=3, wait_per_attempt=.5): for attempt in range(tries): try: # change following to whatever you do to attempt a lock. lock_file_with_timeout(wait_per_attempt) except LockAttemptFailure: # or however the failure is shown pass # here the attempt+1th try failed. else: return # success -- have the lock raise ImTiredError --Scott David Daniels scott.dani...@acm.org -- Chris Green -- http://mail.python.org/mailman/listinfo/python-list
Re: How to retry something with a timeout in Python?
tinn...@isbd.co.uk wrote: This feels like it should be simple but I can't see a clean way of doing it at the moment. I want to retry locking a file for a number of times and then give up, in pseudo-code it would be something like:- for N times try to lock file if successful break out of for loop if we don't have a lock then give up and exit for attempt in range(N): try: lock_file_with_timeout(per_try) # change to what you mean except LockAttemptFailure: # or however the failure is shown pass # here the attempt+1th try failed. else: break # success -- have the lock else: raise ImTiredError # however you handle N attempts w/o success rest_of_code Often it is easiest to stick it in a function: def retry_lock(tries=3, wait_per_attempt=.5): for attempt in range(tries): try: # change following to whatever you do to attempt a lock. lock_file_with_timeout(wait_per_attempt) except LockAttemptFailure: # or however the failure is shown pass # here the attempt+1th try failed. else: return # success -- have the lock raise ImTiredError --Scott David Daniels scott.dani...@acm.org -- http://mail.python.org/mailman/listinfo/python-list
Re: How to retry something with a timeout in Python?
En Tue, 28 Apr 2009 17:56:13 -0300, tinn...@isbd.co.uk escribió: I want to retry locking a file for a number of times and then give up, in pseudo-code it would be something like:- for N times try to lock file if successful break out of for loop if we don't have a lock then give up and exit How does one do this tidily in python? for i in range(N): if try_to_lock_file(): break else: raise RuntimeError, Could not lock file do_real_work() Note the indentation of the `else` clause, it belongs to the `for` loop and is executed only when the iteration is completely exhausted. -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list