Created
July 11, 2013 09:30
-
-
Save schlamar/5973986 to your computer and use it in GitHub Desktop.
Tulip vs. Tornado benchmark.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import timeit | |
import tornloop | |
import tulip | |
loop = tornloop.TornadoLoop() | |
tulip.set_event_loop(loop) | |
def async_func(value): | |
p = tulip.Future() | |
loop.call_later(0.01, p.set_result, value) | |
return p | |
@tulip.coroutine | |
def sub_sub(): | |
yield from async_func(1) | |
yield from async_func(1) | |
@tulip.coroutine | |
def sub(): | |
yield from async_func(1) | |
yield from async_func(1) | |
yield from sub_sub() | |
@tulip.task | |
def main(): | |
yield from async_func(1) | |
yield from sub() | |
yield from sub() | |
def bench(): | |
loop.run_until_complete(main()) | |
if __name__ == '__main__': | |
print(timeit.timeit('bench()', 'from __main__ import bench', number=400)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import timeit | |
import tulip | |
loop = tulip.get_event_loop() | |
def async_func(value): | |
p = tulip.Future() | |
loop.call_later(0.01, p.set_result, value) | |
return p | |
@tulip.coroutine | |
def sub_sub(): | |
yield from async_func(1) | |
yield from async_func(1) | |
@tulip.coroutine | |
def sub(): | |
yield from async_func(1) | |
yield from async_func(1) | |
yield from sub_sub() | |
@tulip.coroutine | |
def main(): | |
yield from async_func(1) | |
yield from sub() | |
yield from sub() | |
def bench(): | |
loop.run_until_complete(main()) | |
if __name__ == '__main__': | |
print(timeit.timeit('bench()', 'from __main__ import bench', number=400)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import functools | |
from tornado import ioloop | |
try: | |
import tulip | |
Base = tulip.AbstractEventLoop | |
except ImportError: | |
Base = object | |
def ignore_args(func): | |
@functools.wraps(func) | |
def wrapper(*args, **kwargs): | |
return func() | |
return wrapper | |
class TornadoLoop(Base): | |
_loop = ioloop.IOLoop().current() | |
def run_forever(self): | |
self._loop.start() | |
def run_until_complete(self, p): | |
p.add_done_callback(ignore_args(self.stop)) | |
self.run_forever() | |
def stop(self): | |
self._loop.stop() | |
def call_soon(self, callback, *args): | |
self._loop.add_callback(callback, *args) | |
def call_later(self, delay, callback, *args): | |
deadline = self._loop.time() + delay | |
callback = functools.partial(callback, *args) | |
self._loop.add_timeout(deadline, callback) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment