Events and file polling in a C extension

Donn Cave donn at
Sat Oct 23 18:24:47 CEST 2004

Quoth Dirk Thierbach <dthierbach at>:
| I am writing an Python 2.3 extension in C, and I would like to create
| events (Tk-style) from an external source. I use a library that
| already parses the events into a C structure. This library can
| also supply file ids for poll() or select() calls (all this is on
| Linux, BTW). 

So what would you do after select() returns with I/O ready?  Can
the library then read and parse, and return the result?

| What's the preferred way to doing this? Can I register my own event
| handlers to the Python runtime, including the file ids to poll/to do
| select on, as in Tcl/Tk? Or do I have to create a new thread, and
| do a blocking select/poll-call inside the thread? If yes, how much
| attention do I have to pay to Py_BEGIN_ALLOW_THREADS/Py_END_ALLOW_THREADS?
| E.g. use them as often as possible, or only at cricitical points?
| How do I create a new thread with the C API, anyway? Or should I wrap
| that into a Python module that calls some auxiliary functions
| in the C module?

I would stay away from threads unless you have a strong need for
concurrency that isn't already satisfied with select/poll.  It seems
to me the simplest thing by far would be the arrangement I suggested
above, with the I/O dispatching in Python and the external C functions
responsible only for data transformation, but it depends a lot on
your application.  Python doesn't come with an event handler model,
except when used with say a graphic toolkit - so for example Python/Tk
will (I assume) look a lot like Tcl/Tk in this respect.

	Donn Cave, donn at

More information about the Python-list mailing list