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

Reply via email to