uvloop icon indicating copy to clipboard operation
uvloop copied to clipboard

Get uv_loop_t pointer

Open ghost opened this issue 6 years ago • 6 comments

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.

ghost avatar Jan 07 '20 01:01 ghost

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?

ghost avatar Jan 11 '20 12:01 ghost

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.

1st1 avatar Jan 14 '20 06:01 1st1

I've been looking for this functionality too! @alexhultman I can pass in a PR for this if you'd like

pranavtbhat avatar Jan 15 '20 17:01 pranavtbhat

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

ghost avatar Jan 15 '20 17:01 ghost

def get_uvloop_ptr(self):
        return PyCapsule_New(<void *>self.uvloop, NULL, NULL)

I guess that's correct?

ghost avatar Jan 17 '20 00:01 ghost

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()

ghost avatar Jan 20 '20 03:01 ghost

Fixed in #310

fantix avatar Sep 09 '22 14:09 fantix