8 Dec
2015
8 Dec
'15
3:52 a.m.
On Mon, Dec 7, 2015 at 3:37 PM Ram Rachum <ram@rachum.com> wrote:
I can't use a with statement, because there's a condition around the place where I acquire the lock. It's something like this:
if condition:
exit_stack.enter_context(get_lock_1())
else:
exit_stack.enter_context(get_lock_2())
You can't put the condition in a context manager's __init__ or __enter__? class CM: def __init__(self, condition): self.lock = lock1 if condition else lock2 def __enter__(self): self.lock.acquire() delf __exit__(self, *info): self.lock.release()