summaryrefslogtreecommitdiff
path: root/tests/run/py35_asyncio_async_def.srctree
blob: 7843b8e1616f9e7670c61897daa546946d95a810 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# mode: run
# tag: asyncio, gh1685, gh2273

PYTHON setup.py build_ext -i
PYTHON main.py


######## setup.py ########

from Cython.Build import cythonize
from distutils.core import setup

setup(
    ext_modules = cythonize("*.pyx"),
)


######## main.py ########

import asyncio
import cy_test
import py_test
from contextlib import closing

async def main():
    await cy_test.say()

with closing(asyncio.new_event_loop()) as loop:
    print("Running Python coroutine ...")
    loop.run_until_complete(main())

    print("Running Cython coroutine ...")
    loop.run_until_complete(cy_test.say())

assert asyncio.iscoroutinefunction(cy_test.cy_async_def_example) == True
assert asyncio.iscoroutinefunction(cy_test.cy_async_def_example) == True
assert asyncio.iscoroutinefunction(py_test.py_async_def_example) == True
assert asyncio.iscoroutinefunction(py_test.py_async_def_example) == True
assert asyncio.iscoroutinefunction(cy_test.cy_def_example) == False
assert asyncio.iscoroutinefunction(py_test.py_def_example) == False

######## cy_test.pyx ########

import asyncio
from py_test import py_async

async def cy_async():
    print("- this one is from Cython")

async def say():
    await cb()

async def cb():
    print("awaiting:")
    await cy_async()
    await py_async()
    print("sleeping:")
    await asyncio.sleep(0.5)
    print("done!")

async def cy_async_def_example():
    return 1

def cy_def_example():
    return 1

######## py_test.py ########

async def py_async():
    print("- and this one is from Python")

async def py_async_def_example():
    return 1

def py_def_example():
    return 1