3

I'm completely new to python, but have an async python app using uvloop which uses a C api module I created which also needs access to the async loop.

1) asyncio does not yet have a c-api for this? Any hacks to get an event loop usable in C? Is this being discussed anywhere?

2) uvloop uses libuv which I am familiar with in C. If I can grab the uv_loop_t pointer I could hook into the loop. I assume I can either:

A) With a PyObject * to uvloop's loop calculate the offset to the uv_loop_t* and use that? Assuming I knew the length of PyObject_HEAD?

libuv_loop = (uv_loop_t*)((void*)(loop)+0x8);

struct __pyx_obj_6uvloop_4loop_Loop {
    PyObject_HEAD
    uv_loop_t *uvloop;

B) Or non hacky modify uvloop to expose the loop pointer. I'm completely clueless here as I've never looked at cython code. Can I create a python function on the loop, call it from my C code and get the C pointer? Like:

(uv_loop_t*)PyObject_CallFunctionObjArgs( getLoop, NULL )

By adding getLoop to here:

https://github.com/MagicStack/uvloop/blob/master/uvloop/loop.pyx

cdef uv.uv_loop_t* _getLoop(self):
    return self.uvloop
Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150
Craftables
  • 236
  • 1
  • 8
  • 1
    `asyncio` itself doesn't know about uvloop. There is a public C API for writing your own coroutines, and you'd access the event loop from C using `PyObject_CallMethod` and friends. Having a public way to access the underlying `uv_loop_t *` sounds like a useful feature request for the `uvloop` Python module. – user4815162342 Apr 13 '18 at 07:23
  • Thanks, I'll make a pull request to uvloop if I can figure out how to make the change in their code. – Craftables Apr 13 '18 at 13:26
  • In the new function, you might want to return the `uv_loop_t *` wrapped in a [capsule](https://docs.python.org/3.6/c-api/capsule.html), a Python object specifically designed to store an opaque pointer. – user4815162342 Apr 14 '18 at 19:22

2 Answers2

9

asyncio has no C API yet.

We have a plan for adding it in future Python versions (3.8 maybe).

Right now you should use PyObject_* api.

uvloop is written in Cython but the library has no Public C API as well. You may access private uvloop API but exposed function names and data structures can be changed in any moment without public notice because they are considered private, users should never use it.

Andrew Svetlov
  • 16,730
  • 8
  • 66
  • 69
  • Great thanks, I see now that I can open my sockets in python and attach callbacks to my C code as well as call create task against the python loop. If you or anyone else has good example libraries to link with best practices please do so. – Craftables Apr 13 '18 at 13:56
0

Was looking for this too, and coincidentally, it just so happens that uvloop added a loop.get_uv_loop_t_ptr() method a few days ago :)

https://github.com/MagicStack/uvloop/pull/310

Now we just have to wait for a new release (v0.17 ?) that includes this PR (or build it ourselves).

pwuertz
  • 1,325
  • 8
  • 15