On 8/8/2014 9:25 AM, Ethan Furman wrote:
On 08/08/2014 04:51 AM, cool-RR wrote:

If I want to acquire a `threading.Lock` using the context manager
protocol,
 is it possible to specify the `blocking` and `timeout` arguments that
 `acquire` would usually take?

Not that I know of, but why would you want to?  There's no built-in 'if'
with a 'with' block -- how would your code know whether it ran or not?

Perhaps defer release, a la a common Go pattern:

with contextlib.ExitStack() as stack:
    acquired = lock.acquire(blocking=False)
    if acquired:
        stack.callback(lock.release)
        do_stuff

--
Neil Cerutti

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to