
August 10th, 2005, 11:45 PM
| | | signals (again)
I see this (or similar) question occasionally looking back through the
archive, but haven't yet seen a definitive answer, so I'm going to ask
it again.
Consider the following:
while True:
do_something_to_files_in_directory(fd)
fcntl(fd, F_NOTFIY, DN_CREATE)
signal.pause()
How do you deal with the signal that occurs after the fcntl and before
the pause? The solution to sleep instead of pause is aesthetically
ugly and doesn't really help. I've thought about examining the stack
trace in the signal handler for SIGIO and responding appropriately, but
that's pretty ugly too. I saw some mention of implementing a try:
construct that would delay receipt of the signal for one atomic python
instruction, and that seemed like a good idea, but I didn't see much
follow up on that. What's the pythonic thing to do here? How can I
guarantee timely response to the creation of a file in the directory
referenced by fd? | 
August 11th, 2005, 02:35 AM
| | | Re: signals (again)
"bill" <bill.pursell@gmail.com> writes:[color=blue]
> What's the pythonic thing to do here? How can I
> guarantee timely response to the creation of a file in the directory
> referenced by fd?[/color]
Use asynchronous calls and/or a separate thread. | 
August 11th, 2005, 07:45 AM
| | | Re: signals (again)
How does that help? I interpret "use asynchronous calls" to mean "use
fcntl to set an FN_NOTIFY on the directory in order to be alerted when
something needs to be done." But the method of doing that which I
outlined above has a critical section in which the incoming signal will
not be noticed. All of the solutions I can think of for getting around
it involve a lot of acrobatics that I'm fairly certain aren't
necessary, and more to the point I don't think they actually solve the
problem. There is always a section in which the signal arrives just
before the pause, so it doesn't wake us up out of the pause as it is
intended to. We can set globals in the signal handler, and then check
them right before the pause, but if they get set after we check, then
we're hosed. I was hoping that perhaps the following code might be
atomic:
if notify_occurred or signal.pause():
but I'm almost certain that it's not.
The problem is localized, so I don't see how invoking a seperate thread
is useful. Could you elaborate on how you think that will help? | 
August 11th, 2005, 01:35 PM
| | | Re: signals (again)
"bill" <bill.pursell@gmail.com> writes:
[color=blue]
> I see this (or similar) question occasionally looking back through the
> archive, but haven't yet seen a definitive answer, so I'm going to ask
> it again.
>
> Consider the following:
>
> while True:
> do_something_to_files_in_directory(fd)
> fcntl(fd, F_NOTFIY, DN_CREATE)
> signal.pause()
>
>
> How do you deal with the signal that occurs after the fcntl and before
> the pause?[/color]
I don't think you can, sorry.
Cheers,
mwh
--
Get out your salt shakers folks, this one's going to take more
than one grain. -- Ator in an Ars Technica news item | 
August 12th, 2005, 07:35 AM
| | | Re: signals (again)
I found a good solution to this problem in Richard Steven's
_Network_Programming_. It seems like everything shows up in Steven's
books! Rather than pausing, you do a blocking read on a pipe. You
only write to the pipe from within the signal handler. However, this
brings up the better question: why was 'pause' ever implemented? No
matter what you do, the signal that you expect to wake you up may occur
immediately prior to the pause, and you'll miss it. Starting that
question in a new thread. |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|