Skip to content

Commit

Permalink
Reorder gyield to optimize for non-timeout case
Browse files Browse the repository at this point in the history
  • Loading branch information
virtuald committed Jun 6, 2016
1 parent 9877dba commit 60651b3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
0.2.4 - 2016-06-06
------------------
* Reorder gyield to optimize non-timeout case

0.2.3 - 2016-05-13
------------------
* tornado.gen.moment doesn't work, use gmoment instead
Expand Down
27 changes: 16 additions & 11 deletions greenado/concurrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,17 +277,15 @@ def gyield(future, timeout=None):
if not future.done():

io_loop = IOLoop.current()

timeout_handle = None
timeout_future = None
wait_future = future

def on_timeout():
timeout_future.set_exception(TimeoutError("Timeout after %s seconds" % timeout))
gr.switch()
if timeout != None and timeout > 0:
# optimization: only do timeout related work if a timeout is happening..

def on_complete(result):
if timeout_handle is not None:
timeout_handle = None
timeout_future = None

def on_complete(result):
if timeout_future.done():
# resolve the future so tornado doesn't complain
try:
Expand All @@ -300,15 +298,22 @@ def on_complete(result):
else:
timeout_future.set_result(True)
io_loop.remove_timeout(timeout_handle)
gr.switch()

if timeout != None and timeout > 0:
gr.switch()

def on_timeout():
timeout_future.set_exception(TimeoutError("Timeout after %s seconds" % timeout))
gr.switch()

wait_future = timeout_future = concurrent.TracebackFuture()
timeout_handle = io_loop.add_timeout(
io_loop.time() + timeout,
on_timeout
)

else:
def on_complete(result):
gr.switch()

io_loop.add_future(future, on_complete)

with NullContext():
Expand Down
2 changes: 1 addition & 1 deletion greenado/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.2.3'
__version__ = '0.2.4'

0 comments on commit 60651b3

Please sign in to comment.