Skip to content
Browse files

General cleanup, and again make it possible to use HTTPServer's multi…

…ple process mechanism.
  • Loading branch information...
1 parent 6ed02a1 commit fae73a53dac6984dbea39bde7794e135da0c614a Eric Naeseth committed Apr 26, 2010
Showing with 12 additions and 14 deletions.
  1. +12 −14 swirl.py
View
26 swirl.py
@@ -20,11 +20,12 @@ def isgeneratorfunction(obj):
__version__ = '0.1.0'
-class CoroutineRunner:
- def __init__(self, io_loop, gen, web_handler):
- self.gen = gen
- self.io_loop = io_loop
+class CoroutineRunner(object):
+ def __init__(self, generator, web_handler=None, io_loop=None):
+ self.gen = generator
self.web_handler = web_handler
+ self.io_loop = io_loop
+
# start the ball rolling...
self.callback_proxy()
@@ -48,6 +49,10 @@ def callback_proxy(self, *args):
self.work = self.gen.send(args)
else:
self.work = self.gen.next()
+
+ if self.io_loop is None:
+ self.io_loop = IOLoop.instance()
+
self.io_loop.add_callback(self.execute_work)
except StopIteration:
if self.web_handler and not self.web_handler._finished:
@@ -70,11 +75,6 @@ def make_asynchronous_decorator(io_loop):
For information on how to use such a decorator, see
`swirl.asynchronous`.
"""
- if io_loop is None:
- io_loop = IOLoop.instance()
-
- # hack because Python 2.x lacks the "nonlocal" keyword
- io_loop = [io_loop]
def asynchronous(coroutine):
"""
@@ -94,12 +94,10 @@ def run_async_routine(*args, **kwargs):
# we check if we're an instancemethod of RequestHandler for better
# intergration
if len(args) > 0 and isinstance(args[0], RequestHandler):
- CoroutineRunner(io_loop[0],
- web_async_coroutine(*args, **kwargs),
- args[0])
+ CoroutineRunner(web_async_coroutine(*args, **kwargs),
+ args[0], io_loop)
else:
- CoroutineRunner(io_loop[0],
- coroutine(*args, **kwargs), None)
+ CoroutineRunner(coroutine(*args, **kwargs), io_loop=io_loop)
return run_async_routine
return asynchronous

0 comments on commit fae73a5

Please sign in to comment.
Something went wrong with that request. Please try again.