1# mode: run 2# tag: asyncio, gh1685 3 4PYTHON setup.py build_ext -i 5PYTHON main.py 6 7 8######## setup.py ######## 9 10from Cython.Build import cythonize 11from distutils.core import setup 12 13setup( 14 ext_modules = cythonize("*.pyx"), 15) 16 17 18######## main.py ######## 19 20import asyncio 21import cy_test 22from contextlib import closing 23 24async def main(): 25 await cy_test.say() 26 27with closing(asyncio.get_event_loop()) as loop: 28 print("Running Python coroutine ...") 29 loop.run_until_complete(main()) 30 31 print("Running Cython coroutine ...") 32 loop.run_until_complete(cy_test.say()) 33 34 35######## cy_test.pyx ######## 36 37import asyncio 38from py_test import py_async 39 40async def cy_async(): 41 print("- this one is from Cython") 42 43async def say(): 44 await cb() 45 46async def cb(): 47 print("awaiting:") 48 await cy_async() 49 await py_async() 50 print("sleeping:") 51 await asyncio.sleep(0.5) 52 print("done!") 53 54 55######## py_test.py ######## 56 57async def py_async(): 58 print("- and this one is from Python") 59