| 1 |
# mode: run |
| 2 |
# tag: asyncio, gh1685, gh2273 |
| 3 |
|
| 4 |
PYTHON setup.py build_ext -i |
| 5 |
PYTHON main.py |
| 6 |
|
| 7 |
|
| 8 |
######## setup.py ######## |
| 9 |
|
| 10 |
from Cython.Build import cythonize |
| 11 |
from distutils.core import setup |
| 12 |
|
| 13 |
setup( |
| 14 |
ext_modules = cythonize("*.pyx"), |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
######## main.py ######## |
| 19 |
|
| 20 |
import asyncio |
| 21 |
import cy_test |
| 22 |
import py_test |
| 23 |
from contextlib import closing |
| 24 |
|
| 25 |
async def main(): |
| 26 |
await cy_test.say() |
| 27 |
|
| 28 |
with closing(asyncio.new_event_loop()) as loop: |
| 29 |
print("Running Python coroutine ...") |
| 30 |
loop.run_until_complete(main()) |
| 31 |
|
| 32 |
print("Running Cython coroutine ...") |
| 33 |
loop.run_until_complete(cy_test.say()) |
| 34 |
|
| 35 |
assert asyncio.iscoroutinefunction(cy_test.cy_async_def_example) == True |
| 36 |
assert asyncio.iscoroutinefunction(cy_test.cy_async_def_example) == True |
| 37 |
assert asyncio.iscoroutinefunction(py_test.py_async_def_example) == True |
| 38 |
assert asyncio.iscoroutinefunction(py_test.py_async_def_example) == True |
| 39 |
assert asyncio.iscoroutinefunction(cy_test.cy_def_example) == False |
| 40 |
assert asyncio.iscoroutinefunction(py_test.py_def_example) == False |
| 41 |
|
| 42 |
######## cy_test.pyx ######## |
| 43 |
|
| 44 |
import asyncio |
| 45 |
from py_test import py_async |
| 46 |
|
| 47 |
async def cy_async(): |
| 48 |
print("- this one is from Cython") |
| 49 |
|
| 50 |
async def say(): |
| 51 |
await cb() |
| 52 |
|
| 53 |
async def cb(): |
| 54 |
print("awaiting:") |
| 55 |
await cy_async() |
| 56 |
await py_async() |
| 57 |
print("sleeping:") |
| 58 |
await asyncio.sleep(0.5) |
| 59 |
print("done!") |
| 60 |
|
| 61 |
async def cy_async_def_example(): |
| 62 |
return 1 |
| 63 |
|
| 64 |
def cy_def_example(): |
| 65 |
return 1 |
| 66 |
|
| 67 |
######## py_test.py ######## |
| 68 |
|
| 69 |
async def py_async(): |
| 70 |
print("- and this one is from Python") |
| 71 |
|
| 72 |
async def py_async_def_example(): |
| 73 |
return 1 |
| 74 |
|
| 75 |
def py_def_example(): |
| 76 |
return 1 |