Commit 49cd0a97 authored by Denis Bilenko's avatar Denis Bilenko

proc.Proc.wait: simplify implementation and change signature a bit: exception...

proc.Proc.wait: simplify implementation and change signature a bit: exception instead of *throw_args
parent 75fe7367
...@@ -421,7 +421,7 @@ class Source(object): ...@@ -421,7 +421,7 @@ class Source(object):
core.timer(0, self._do_send, links, consult) core.timer(0, self._do_send, links, consult)
raise raise
def wait(self, timeout=None, *throw_args): def wait(self, timeout=None, exception=greenlet.TimeoutError):
"""Wait until send() or send_exception() is called or `timeout' has """Wait until send() or send_exception() is called or `timeout' has
expired. Return the argument of send or raise the argument of expired. Return the argument of send or raise the argument of
send_exception. If timeout has expired, None is returned. send_exception. If timeout has expired, None is returned.
...@@ -435,35 +435,46 @@ class Source(object): ...@@ -435,35 +435,46 @@ class Source(object):
return self.value return self.value
else: else:
greenlet.getcurrent().throw(*self._exc) greenlet.getcurrent().throw(*self._exc)
if timeout is not None: elif timeout is None:
timer = greenlet.Timeout(timeout, *throw_args) waiter = Waiter()
timer.__enter__() self.link(waiter)
if timeout==0: try:
if timer.__exit__(None, None, None): return waiter.wait()
return finally:
else: self.unlink(waiter)
try: elif timeout <= 0:
greenlet.getcurrent().throw(timer.exception) if exception is None:
except: return
if not timer.__exit__(*sys.exc_info()): else:
raise raise exception
return else:
# what follows is:
# with greenlet.Timeout(timeout, *throw_args):
# waiter = Waiter()
# self.link(waiter)
# try:
# return waiter.wait()
# finally:
# self.unlink(waiter)
# however, with statement is hand decompiled to make it 2.4 compatible
timer = greenlet.Timeout(timeout, exception)
EXC = True EXC = True
try:
try: try:
waiter = Waiter()
self.link(waiter)
try: try:
return waiter.wait() waiter = Waiter()
finally: self.link(waiter)
self.unlink(waiter) try:
except: return waiter.wait()
EXC = False finally:
if timeout is None or not timer.__exit__(*sys.exc_info()): self.unlink(waiter)
raise except:
finally: EXC = False
if timeout is not None and EXC: if not timer.__exit__(*sys.exc_info()):
timer.__exit__(None, None, None) raise
finally:
if EXC:
timer.__exit__(None, None, None)
# QQQ allow exception to be a tuple?
class Waiter(object): class Waiter(object):
...@@ -677,6 +688,7 @@ class RunningProcSet(object): ...@@ -677,6 +688,7 @@ class RunningProcSet(object):
for x in self.procs: for x in self.procs:
if x.greenlet == item: if x.greenlet == item:
return True return True
# hack Proc's __hash__ and __eq__ to avoid special casing this?
else: else:
return item in self.procs return item in self.procs
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment