Get uv_loop_t pointer
Hello,
For integration purposes I need to get the current underlying uv_loop_t pointer, preferably as a PyCapsule, from uvloop. I need that pointer for use by my python extension and for it to seamlessly integrate with asyncio scripts.
Do you think you could add such a function, or point me in the direction where I can add it? It should be a minimal change.
I have experimented with passing a Loop object to my extension, accessing the PyTypeObject, confirming the tp_name and tp_itemsize.
From there I planned on accessing the uv_loop_t pointer from the PyObject (the loop pointer looks to be at the top of the object?) but I don't think I have the correct pointer yet. I'm not so sure about how this Cython language works.
Any hints?
You can probably add a getter like this in loop.pyx:
def get_uvloop_ptr(self):
return <uint64_t>self.uvloop
You can then call it and cast the returned int to a pointer.
I've been looking for this functionality too! @alexhultman I can pass in a PR for this if you'd like
Please make it PyCapsule_New(ptr, NULL, NULL) instead so it becomes a proper thing.
https://docs.python.org/3/c-api/capsule.html
Python capsules are like Extern in V8, they are objects that hold raw pointers.
Then any extension can PyCapsule_GetPointer that thing
def get_uvloop_ptr(self):
return PyCapsule_New(<void *>self.uvloop, NULL, NULL)
I guess that's correct?
I actually end up doing this:
asyncio.set_event_loop(asyncio.SelectorEventLoop(uWS.Selector()))
where uWS.Selector() is a minimal object wrapping my internal libuv implementation.
This works for me, but it could still be useful to also have uvloop.get_loop_ptr()
Fixed in #310