Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get uv_loop_t pointer #307

Closed
ghost opened this issue Jan 7, 2020 · 7 comments
Closed

Get uv_loop_t pointer #307

ghost opened this issue Jan 7, 2020 · 7 comments

Comments

@ghost
Copy link

ghost commented Jan 7, 2020

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
Copy link
Author

ghost commented Jan 11, 2020

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?

@1st1
Copy link
Member

1st1 commented Jan 14, 2020

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.

@pranavtbhat
Copy link
Contributor

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

@ghost
Copy link
Author

ghost commented Jan 15, 2020

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
Copy link
Author

ghost commented Jan 17, 2020

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

I guess that's correct?

@ghost
Copy link
Author

ghost commented Jan 20, 2020

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

@fantix
Copy link
Member

fantix commented Sep 9, 2022

Fixed in #310

@fantix fantix closed this as completed Sep 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants