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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
# mode: run
# tag: asyncio, pep492
"""
PYTHON setup.py build_ext -i
PYTHON test_from_import.py
PYTHON test_import.py
PYTHON test_async_def.py
PYTHON test_async_def_future.py
PYTHON test_all.py
"""
######## setup.py ########
from Cython.Build.Dependencies import cythonize
from distutils.core import setup
setup(
ext_modules = cythonize("*.pyx"),
)
######## test_from_import.py ########
import from_asyncio_import
import asyncio
from contextlib import closing
new_event_loop = asyncio.new_event_loop
def runloop(task):
with closing(new_event_loop()) as loop:
result = loop.run_until_complete(task())
assert 3 == result, result
######## test_import.py ########
import import_asyncio
import asyncio
from contextlib import closing
new_event_loop = asyncio.new_event_loop
def runloop(task):
with closing(new_event_loop()) as loop:
result = loop.run_until_complete(task())
assert 3 == result, result
######## test_async_def.py ########
import async_def
import asyncio
from contextlib import closing
def runloop(task):
with closing(asyncio.new_event_loop()) as loop:
result = loop.run_until_complete(task())
assert 3 == result, result
runloop(async_def.wait3)
######## test_async_def_future.py ########
from async_def_future import await_future
import asyncio
from contextlib import closing
def runloop():
with closing(asyncio.new_event_loop()) as loop:
task, events, expected = await_future(loop)
result = loop.run_until_complete(task())
assert events == expected, 'expected %s, got %s' % (expected, events)
runloop()
######## test_all.py ########
import asyncio
from contextlib import closing
new_event_loop = asyncio.new_event_loop
def runloop(task):
with closing(new_event_loop()) as loop:
result = loop.run_until_complete(task())
assert 3 == result, result
import import_asyncio
import from_asyncio_import
import async_def
runloop(async_def.wait3) # 1c)
runloop(async_def.wait3) # 2c)
runloop(async_def.wait3) # 3c)
from collections.abc import Generator
assert isinstance(from_asyncio_import.wait3(), Generator)
assert isinstance(import_asyncio.wait3(), Generator)
assert isinstance((lambda:(yield))(), Generator)
from collections.abc import Awaitable
assert isinstance(async_def.wait3(), Awaitable)
from collections.abc import Coroutine
assert isinstance(async_def.wait3(), Coroutine)
######## import_asyncio.pyx ########
# cython: binding=True
try:
from types import coroutine as types_coroutine
except ImportError:
types_coroutine = lambda f:f
import asyncio
@types_coroutine
def wait3():
counter = 0
for i in range(3):
print(counter)
yield from asyncio.sleep(0.01)
counter += 1
return counter
######## from_asyncio_import.pyx ########
# cython: binding=True
try:
from types import coroutine as types_coroutine
except ImportError:
types_coroutine = lambda f:f
from asyncio import sleep
@types_coroutine
def wait3():
counter = 0
for i in range(3):
print(counter)
yield from sleep(0.01)
counter += 1
return counter
######## async_def.pyx ########
# cython: binding=True
import asyncio
async def wait3():
counter = 0
for i in range(3):
print(counter)
await asyncio.sleep(0.01)
counter += 1
return counter
######## async_def_future.pyx ########
# cython: binding=True
import asyncio
def await_future(loop):
events = []
async def worker():
print("worker running")
fut = asyncio.Future()
def setval():
print("setval running")
events.append('setval')
fut.set_result(123)
print("setval done")
events.append('setup')
loop.call_later(0.2, setval)
events.append(await fut)
print("worker done")
async def test():
print("Test running")
await worker()
expected = ['setup', 'setval', 123]
return test, events, expected
|