473,396 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Events and file polling in a C extension

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

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?

As I am comparatively new to Python and the C API, I would be glad for
some boilerplate C-code (if there is any available somewhere) I could
use as a starting point, so I don't have to re-invent the wheel myself
another time :-)

Thanks for any help,

- Dirk
Jul 18 '05 #1
4 2420
Quoth Dirk Thierbach <dt********@gmx.de>:
| 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, do**@drizzle.com
Jul 18 '05 #2
Donn Cave <do**@drizzle.com> wrote:
Quoth Dirk Thierbach <dt********@gmx.de>: So what would you do after select() returns with I/O ready? Can
the library then read and parse, and return the result?
Yes.
I would stay away from threads unless you have a strong need for
concurrency that isn't already satisfied with select/poll.
So would I, but at the moment I don't see how, unless Python supports
some sort of event handling.
It seems to me the simplest thing by far would be the arrangement I
suggested above,
Which one? Handling select in the main program? That doesn't work,
because...
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.


.... I am exactly after something that looks like Python/Tk with
respect to Tk-bindings. I want to create an object, let the main
program do some bindings on that object, and then the main program
can go on to do whatever it likes. The object should dispatch any
events that arrive on the file descriptor by invoking the bindings,
just like the widget bindings in Python/Tk.

Of course I could create some sort of main loop myself, but that
seems inelegant and not portable. After all, Python/Tk doesn't
require you to call some sort of main loop, too.

- Dirk
Jul 18 '05 #3

Dirk Thierbach <dt********@gmx.de> wrote:

Donn Cave <do**@drizzle.com> wrote:
Quoth Dirk Thierbach <dt********@gmx.de>:
So what would you do after select() returns with I/O ready? Can
the library then read and parse, and return the result?


Yes.
I would stay away from threads unless you have a strong need for
concurrency that isn't already satisfied with select/poll.


So would I, but at the moment I don't see how, unless Python supports
some sort of event handling.

I believe that most languages support event handling in one way or
another. Here is one way...

def handle_event(event):
if event.typ == 'new_connection':
#handle a new connection
elif event.typ == ...

Or, if you want to use something that already has much of this designed
and built for sockets, check out asyncore or asynchat. Use
asyncore.loop(timeout, 1) to poll the sockets every 1/100th of a second
or so, and you should be good.

It seems to me the simplest thing by far would be the arrangement I
suggested above,


Which one? Handling select in the main program? That doesn't work,
because...
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.


... I am exactly after something that looks like Python/Tk with
respect to Tk-bindings. I want to create an object, let the main
program do some bindings on that object, and then the main program
can go on to do whatever it likes. The object should dispatch any
events that arrive on the file descriptor by invoking the bindings,
just like the widget bindings in Python/Tk.


These are not hard to make work reasonably. They can be hard to do well
and in a general fashion. If you want a general event-based framework,
give Twisted a look (though it does seem to be a bit more socket related).
Of course I could create some sort of main loop myself, but that
seems inelegant and not portable. After all, Python/Tk doesn't
require you to call some sort of main loop, too.


While I do not develop with Tk, I believe you are incorrect. Checking
Idle, I notice a call to: root.mainloop(), which seems to be the event
handler loop.

In regards to writing your own main loop; what is so inelegant and not
portable about it? Having done so more than a dozen times, I would say
that writing your own main loop is usually the way to go, unless you
need to hook into some GUI toolkit or a pre-existing event framework, at
which point you can use the toolkit or framework's event processor to
handle dispatch.

- Josiah

Jul 18 '05 #4

Josiah Carlson <jc******@uci.edu> wrote:
I believe that most languages support event handling in one way or
another. Here is one way...

def handle_event(event):
if event.typ == 'new_connection':
#handle a new connection
elif event.typ == ...

Or, if you want to use something that already has much of this designed
and built for sockets, check out asyncore or asynchat. Use
asyncore.loop(timeout, 1) to poll the sockets every 1/100th of a second
or so, and you should be good.


That is not to say that asyncore.loop(timeout, 1) will poll the sockets
every 100th a second, just that you should make that call about every
1/100th a second (timeout of 0 would be a true poll).

- Josiah

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Daniel Mueller | last post by:
Hello Fellow Python Programmers, I have the following problem: i want to read the content of a file every 10 seconds. now what is the best funktion to do that? to open the file, read it and...
0
by: Ganesh Kolappan via .NET 247 | last post by:
Hi I am trying to populate a <asp:dropdownlist> in a XSLT file withdatasource pointing to a C# codebehind file method which returnsa dataview. I am using XSLT extension object. But I am...
4
by: Colleyville Alan | last post by:
I found the transfertext function and found that it bombs if I do not use the extension "txt". But the application I am writing the info for has a different file extenstion. What VBA command will...
1
by: D. Yates | last post by:
Hi, I am looking for an example of how to extract bitmap images from an embedded resource file (a file with *.res extension, which can be viewed inside of the ide and can hold bitmaps, icons,...
1
by: news.microsoft.com | last post by:
Hello, I want to get a list of file types from a big list of extensions. I know there exists an api call (see below), but it only works when you have a file name. SHGetFileInfo(FileName, 0,...
3
by: Phoe6 | last post by:
Hi all, I had a filesystem crash and when I retrieved the data back the files had random names without extension. I decided to write a script to determine the file extension and create a newfile...
1
by: jfmarto | last post by:
Hi, I'm doing a program with VS.2005 and compact framework 2.0. How can i define which program opens automatically a file (with a specific extension). Thanks
4
by: Unnamed | last post by:
Hi all Can anyone please tell me the best way to get the whether an uploaded file is a csv file and also to check if the uploaded file has more than 1 column. i have tried to get filetype but am...
1
by: mwcapps | last post by:
I'm writing an import application that import .csv files into my SQL Server 2005 database. When the import is complete I need to change that files extension from .csv to .imp with the same path. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.