Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Cython with Asyncio (Python 3.4)

Did someone managed to make Cython work with an Asyncio coroutine ? I have a very trivial example that works well in Python, and refuses to work in Cython : it's the following single file that I compile with Cython and execute. The execution starts correctly but fails to run the coroutine. It seems like Cython modifies the type of the "sometask" coroutine, which is then not treated as usual by Asyncio and Inspect.

#!/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 : Currently I make it "work" by modifying in an ugly way the asyncio/tasks.py file in places where it checks if the object is a generator, while in fact Cython made it a built-in function. This Cython object will still do the work of an asyncio coroutine, even though it has a different type than expected.

like image 320
MoriB Avatar asked Apr 16 '15 13:04

MoriB


Video Answer


1 Answers

Fortunately, Stefan Behnel made a workaround for this issue in his last version of Cython's master branch.

EDIT : The commit that solves all the issues is c8a2d30806b4e479515d44ee827b3a1651ac8731

A maybe more appropriate solution would be on the Python side, particularly in Asyncio, to identify generators without checking their type. A full solution requires Python 3.4.2 (for sure > Python 3.4.0) Link to more details : https://groups.google.com/forum/#!topic/cython-users/g146SZHxRyM

like image 111
MoriB Avatar answered Sep 19 '22 20:09

MoriB