Skip to content

Instantly share code, notes, and snippets.

@c4urself
Created October 11, 2019 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save c4urself/a6390d2e044a16ca5deae302a61cca69 to your computer and use it in GitHub Desktop.
Save c4urself/a6390d2e044a16ca5deae302a61cca69 to your computer and use it in GitHub Desktop.
import asyncio
import tornado
from tornado import gen, ioloop
SLEEPY = 0.2
async def native_coroutine():
print('hi from native')
await asyncio.sleep(SLEEPY)
@gen.coroutine
def decorated_coroutine():
print('hi from tornado decorated coroutine')
yield gen.sleep(SLEEPY)
@gen.coroutine
def decorated_calls_native_coroutine():
print('calling native from decorated...')
yield native_coroutine()
async def native_calls_decorated_coroutine():
print('calling decorated from native...')
await decorated_coroutine()
@gen.coroutine
def stacked():
print('calling native from decorated (stacked)...')
yield native_calls_decorated_coroutine()
if __name__ == '__main__':
print("TEST 1: regular tornado decorated on tornado loop")
ioloop.IOLoop.current().run_sync(decorated_coroutine)
print("TEST 2: native asyncio coroutine on tornado loop")
ioloop.IOLoop.current().run_sync(native_coroutine)
print("TEST 3: decorated calling native on tornado loop")
ioloop.IOLoop.current().run_sync(decorated_calls_native_coroutine)
print("TEST 4: native asyncio calling decorated on tornado loop")
ioloop.IOLoop.current().run_sync(native_calls_decorated_coroutine)
print("TEST 5: stacked")
ioloop.IOLoop.current().run_sync(stacked)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment