Re: Specifying `blocking` and `timeout` when acquiring lock as a context manager

2014-08-08 Thread Chris Angelico
On Sat, Aug 9, 2014 at 4:35 AM, Neil D. Cerutti wrote: > Doesn't any natural looking use of blocking=False suffer from the same race > condition? What's the correct way to use it? Actually, I don't know. I try to avoid any form of thread locking where possible, and I don't remember the last time

Re: Specifying `blocking` and `timeout` when acquiring lock as a context manager

2014-08-08 Thread Ian Kelly
On Fri, Aug 8, 2014 at 7: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

Re: Specifying `blocking` and `timeout` when acquiring lock as a context manager

2014-08-08 Thread Chris Kaynor
On Fri, Aug 8, 2014 at 11:35 AM, Neil D. Cerutti wrote: > On 8/8/2014 12:16 PM, Chris Angelico wrote: > >> On Sat, Aug 9, 2014 at 2:05 AM, Neil D. Cerutti >> wrote: >> >>> Perhaps defer release, a la a common Go pattern: >>> >>> with contextlib.ExitStack() as stack: >>> acquired = lock.acqu

Re: Specifying `blocking` and `timeout` when acquiring lock as a context manager

2014-08-08 Thread Neil D. Cerutti
On 8/8/2014 2:35 PM, Neil D. Cerutti wrote: Here's another attempt at context managing: @contextlib.contextmanager def release_if_acquired(lock, blocking=True, timeout=-1): acquired = lock.acquire(blocking, timeout) if acquired: yield acquired lock.release() e

Re: Specifying `blocking` and `timeout` when acquiring lock as a context manager

2014-08-08 Thread Neil D. Cerutti
On 8/8/2014 12:16 PM, Chris Angelico wrote: On Sat, Aug 9, 2014 at 2:05 AM, Neil D. Cerutti wrote: 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)

Re: Specifying `blocking` and `timeout` when acquiring lock as a context manager

2014-08-08 Thread Chris Angelico
On Sat, Aug 9, 2014 at 2:05 AM, Neil D. Cerutti wrote: > 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 There's a race condition i

Re: Specifying `blocking` and `timeout` when acquiring lock as a context manager

2014-08-08 Thread Neil D. Cerutti
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 y

Re: Specifying `blocking` and `timeout` when acquiring lock as a context manager

2014-08-08 Thread Ethan Furman
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

Specifying `blocking` and `timeout` when acquiring lock as a context manager

2014-08-08 Thread cool-RR
Hi all, 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? Thanks, Ram. -- https://mail.python.org/mailman/listinfo/python-list