`@asyncio.coroutine` not running?
See original GitHub issueConsider test_cy2.pyx
:
import asyncio
loop = asyncio.get_event_loop()
async def handler():
print('Before Sleep')
await asyncio.sleep(1)
print('After Sleep')
return 1
async def run():
print('Run started')
res = await handler()
assert res == 1
print('Run ended')
loop.stop()
loop.run_until_complete(asyncio.ensure_future(run()))
loop.run_forever()
$ python -c 'import os; import pyximport; pyximport.install(); os.chdir("/tmp"); import test_cy2' features/aiohttp b6569d6 ✗
/home/vampas/.dotfiles/.ext/pyenv/versions/Raas-3.5.2-features-aiohttp/lib/python3.5/site-packages/Cython/Distutils/old_build_ext.py:30: UserWarning: Cython.Distutils.old_build_ext does not properly handle dependencies and is deprecated.
"Cython.Distutils.old_build_ext does not properly handle dependencies "
Run started
Before Sleep
After Sleep
Run ended
$
Now consider test_cy3.pyx
:
import asyncio
@asyncio.coroutine
def handler():
print('Before Sleep')
yield from asyncio.sleep(1)
print('After Sleep')
return 1
@asyncio.coroutine
def run():
print('Run started')
res = yield from handler()
assert res == 1
print('Run ended')
loop.stop()
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.ensure_future(run()))
loop.run_forever()
$ python -c 'import os; import pyximport; pyximport.install(); os.chdir("/tmp"); import test_cy3' features/aiohttp b6569d6 ✗
/home/vampas/.dotfiles/.ext/pyenv/versions/Raas-3.5.2-features-aiohttp/lib/python3.5/site-packages/Cython/Distutils/old_build_ext.py:30: UserWarning: Cython.Distutils.old_build_ext does not properly han
dle dependencies and is deprecated.
"Cython.Distutils.old_build_ext does not properly handle dependencies "
^CTraceback (most recent call last):
File "<string>", line 1, in <module>
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 664, in _load_unlocked
File "<frozen importlib._bootstrap>", line 634, in _load_backward_compatible
File "/home/vampas/.dotfiles/.ext/pyenv/versions/Raas-3.5.2-features-aiohttp/lib/python3.5/site-packages/pyximport/pyximport.py", line 445, in load_module
language_level=self.language_level)
File "/home/vampas/.dotfiles/.ext/pyenv/versions/Raas-3.5.2-features-aiohttp/lib/python3.5/site-packages/pyximport/pyximport.py", line 217, in load_module
mod = imp.load_dynamic(name, so_path)
File "/home/vampas/.dotfiles/.ext/pyenv/versions/Raas-3.5.2-features-aiohttp/lib/python3.5/imp.py", line 342, in load_dynamic
return _load(spec)
File "test_cy3.pyx", line 22, in init test_cy3 (/home/vampas/.pyxbld/temp.linux-x86_64-3.5/pyrex/test_cy3.c:2118)
loop.run_forever()
File "/home/vampas/.dotfiles/.ext/pyenv/versions/3.5.2/lib/python3.5/asyncio/base_events.py", line 345, in run_forever
self._run_once()
File "/home/vampas/.dotfiles/.ext/pyenv/versions/3.5.2/lib/python3.5/asyncio/base_events.py", line 1276, in _run_once
event_list = self._selector.select(timeout)
File "/home/vampas/.dotfiles/.ext/pyenv/versions/3.5.2/lib/python3.5/selectors.py", line 441, in select
fd_event_list = self._epoll.poll(timeout, max_ev)
KeyboardInterrupt
$
I had to manually interrupt the loop and nothing ran…
Issue Analytics
- State:
- Created 7 years ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Python Asyncio is not running new coroutine ... - Stack Overflow
1>Python Asyncio is not running new coroutine using asyncio.run_coroutine_threadsafe. Below is the code testing performed on Mac.
Read more >Coroutines and Tasks — Python 3.11.1 documentation
To actually run a coroutine, asyncio provides the following mechanisms: ... The asyncio.create_task() function to run coroutines concurrently as asyncio Tasks .
Read more >How to Run an Asyncio Coroutine in Python
You can run an asyncio coroutine via the run() function, by awaiting it within another coroutine, or by scheduling it as Task.
Read more >Common Mistakes Using Python3 asyncio
Common Mistakes Using Python3 asyncio · 1. Introduction · 2. RuntimeWarning: coroutine foo was never awaited · 3. Task was destroyed but it...
Read more >9.4. AsyncIO Coroutine - Python
9.4.8. Error: Running Coroutine Functions¶. Only coroutine objects can be run. It is not possible to run coroutine function.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
So, I’ve been debugging my problem with no solution in sight but I have some test applications which do not corroborate with your first reply. The code can be found here, but basically(transcript of the repo’s readme file with the conclusions).
Versions Used
testapp1
Run testapp1 in non compiled mode:
Now test the old and the new asyncio syntax handlers
Now, let’s install testapp1 which will compile all code using cython:
And now, let’s run the compiled version of testapp1:
Now test the old and the new asyncio syntax handlers:
And the traceback on the server side:
testapp2
testapp2 just imports the
inspect
module intestapp2.app
to try and trigger the cython asyncio patching.Run testapp2 in non compiled mode:
Now test the old and the new asyncio syntax handlers
And now, let’s run the compiled version of testapp2:
Now test the old and the new asyncio syntax handlers:
And the traceback on the server side:
testapp3
testapp3 just imports the
inspect
module on the request handlers modules to try and trigger the cython asyncio patching.Run testapp3 in non compiled mode:
Now test the old and the new asyncio syntax handlers
And now, let’s run the compiled version of testapp3:
Now test the old and the new asyncio syntax handlers:
And the traceback on the server side:
testapp4
testapp4 decorates the new
async def
syntax handler with@asyncio.coroutine
:Run testapp4 in non compiled mode:
Now test the old and the new asyncio syntax handlers
And now, let’s run the compiled version of testapp4:
Now test the old and the new asyncio syntax handlers:
On this
testapp4
we can safely remove theinspect
import from the module where theaiohttp
web application is setup but not from any of the handlers.“Yeah, if we use 3rd party libs which use the asyncio.coroutine, we’re still stuck” It’s (usually) enough if one of the Cython modules in the system imports these modules in order to activate the inspect integration, as all modules built with the same Cython version will share the same internal type implementation.
“This example is to mimic someone using the 3.5 async def/await syntax while having to work with libraries with pre 3.5 syntax…” Note that you can use async/await in Cython modules even if the Python runtime does not support it.