examples of hooking non-blocking c code

284 views
Skip to first unread message

lukatmyshu

unread,
Apr 28, 2011, 3:01:35 PM4/28/11
to gevent: coroutine-based Python network library
From reading the original post of why gevent was created
http://blog.gevent.org/2010/02/27/why-gevent/, it appears that one of
the goals was to hook in an existing c-library that was written using
libevent (I'm not concerned about the impending switch to libev ...
can either rewrite it to use libev, or use libev's libevent
compatability layer)

Are there any examples of how to do that? Is this something that is
supported by gevent still? Thanks.

vijay

Denis Bilenko

unread,
Apr 28, 2011, 3:31:44 PM4/28/11
to gev...@googlegroups.com

Yes, this shouldn't be hard. Link gevent against libev shared library.
Then link your C extension against libev shared library.

If you're in a single-threaded program and only need one event loop then just
use the default event loop in the your C module. The Hub created in
the main thread
of Python program also uses the default loop.

lukatmyshu

unread,
Apr 28, 2011, 4:03:56 PM4/28/11
to gevent: coroutine-based Python network library
Apologies because I didn't actually describe what I'd like to
accomplish fully.

I actually want to wrap the c library in python. Is it possible to do
that in a way where when my python code calls into the c library, it
causes the co-routine to yield? Did that make sense?

vijay

On Apr 28, 12:31 pm, Denis Bilenko <denis.bile...@gmail.com> wrote:

Randall Leeds

unread,
Apr 29, 2011, 3:01:29 PM4/29/11
to gev...@googlegroups.com
Looking at the gevent.hub module and taking gevent.socket as an
example it seems that you can switch greenlets in a safe way through
the hub so long as you have something libev can deal with to trigger
the greenlet to resume. For io this is handled by
get_hub().loop.io(...).

I'm not familiar with libev internals. If you can't control the C code
so that your calls through the python binding can eventually call
get_hub().wait() then perhaps you need to isolate the module calls to
a separate thread. The C module code can then run without a greenlet
context. Some of your binding would be cython which bridges between
the greenlet calling the operation and the C module code servicing it.

That's the most understanding I can get staring at this code for a
half hour. I can't actually even find documentation for libev. Maybe
it's one of those "read the code" libraries (I hear the API is
simple). Anyway. Hopefully Denis can chime in again.

Does this sound like what you have in mind, Vijay?

-R

Denis Bilenko

unread,
Apr 29, 2011, 11:53:21 PM4/29/11
to gev...@googlegroups.com
On Fri, Apr 29, 2011 at 3:03 AM, lukatmyshu <lukat...@gmail.com> wrote:
> I actually want to wrap the c library in python. Is it possible to do
> that in a way where when my python code calls into the c library, it
> causes the co-routine to yield? Did that make sense?

Sorry, not quite.

BTW, here's an example of a C library integrating with gevent:
http://initd.org/psycopg/docs/advanced.html#support-to-coroutine-libraries

On Sat, Apr 30, 2011 at 2:01 AM, Randall Leeds <randal...@gmail.com> wrote:
> That's the most understanding I can get staring at this code for a
> half hour. I can't actually even find documentation for libev. Maybe
> it's one of those "read the code" libraries (I hear the API is
> simple).

Libev has very good documentation:
http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod

Reply all
Reply to author
Forward
0 new messages