Using Cython with Asyncio (Python 3.4)

Did anyone help get Cython to work with Asyncio coroutine? I have a very trivial example that works well in Python and refuses to work in Cython: this is the next separate file that I compile with Cython and execute. The execution starts correctly, but the coroutine does not start. Cython seems to change the type of "sometask" coroutine, which is then not handled by Asyncio and Inspect as usual.

#!/usr/bin/env python import asyncio @asyncio.coroutine def sometask(): counter = 0 while True: print(counter) yield from asyncio.sleep(1) counter += 1 def runloop(): loop = asyncio.get_event_loop() asyncio.async(sometask()) try: print('Start loop') loop.run_forever() except KeyboardInterrupt: print('Aborted by user') loop.close() 

UPDATE: I am currently doing this β€œwork” by modifying in an ugly way the asyncio / tasks.py file in those places where it checks to see if the object is a generator, but in fact Cython made it an inline function. This Cython object will still do the work of asyncio coroutine, although it has a different type than expected.

+6
source share
1 answer

Fortunately, Stefan Bechnel made a workaround for this problem in his latest version of the Keaton master branch.

EDIT: commit that solves all problems - c8a2d30806b4e479515d44ee827b3a1651ac8731

Perhaps a more appropriate solution would be on the Python side, especially in Asyncio, to identify generators without checking their type. A complete solution requires Python 3.4.2 (required> Python 3.4.0) Link to more detailed information: https://groups.google.com/forum/#!topic/cython-users/g146SZHxRyM

+2
source

Source: https://habr.com/ru/post/985266/


All Articles