Trapping Alarm Out Of Blocking READ 
Author Message
 Trapping Alarm Out Of Blocking READ

In LINUX, I'm trying to trap out of a blocking read to a tty device
after a finite number of seconds.  In abbreviated form, my code reads:

--- start of code

import termios, TERMIOS
import signal

term_fd=open("/dev/ttyS0","rw")
fd=term_fd.fileno()

(...termios.tcsetattr(...) to set up blocking read )

try:
        no_char = 1
        signal.alarm(5)
        in_char = term_fd.read(no_char)
except:
        (didn't get the character,  alarm rescued us)
        no_char = 0

(test the value of "no_char" to determine if the read was successful)

--- end of code

After waiting 5 seconds for the read to complete, python 1.4 ends the
code with an "Alarm clock" message, i.e. the exception wasn't trapped
and returns to the teminal prompt. Can someone tell me if I've got the
proper logic for my method.  If so, how do I properly trap the alarm;
else, how should I be doing this.

Thanks for the help,
Ted



Mon, 24 Apr 2000 03:00:00 GMT  
 Trapping Alarm Out Of Blocking READ

Quote:

>In LINUX, I'm trying to trap out of a blocking read to a tty device
>after a finite number of seconds.  In abbreviated form, my code reads:

        signals don't raise an exception; you have to set up a handler
function with signal.signal(signal.SIGALARM, myhandler).  This
function gets called when the signal is received by the Python
process.  (See the LibRef for the signal module:
http://www.python.org/doc/lib/node67.html)

        However, a better approach may be the select module, which
provides an interface to the Unix select() function.  select.select()
is passed 3 lists containing file objects, and returns 3 different
lists containing those objects that are ready for reading or writing.
select.select() has an optional timeout parameter, and will return
either when an object becomes ready for I/O or when time runs out.
For example, this opens /dev/tty, and prints "Type something!" every 5
seconds until you type something.

import os, select

# I'm not sure it's a good idea to use C stdio on things like /dev/tty
# or /dev/ttyS0.  Anyone know?
term_fd=open('/dev/tty', 'r')

while (1):
    # The 3 lists are for: reading, writing, exceptional conditions.
    # We only care about reading in this case, so the last 2 lists
    # are of no interest to us.
    read_list, dummy, dummy = select.select([term_fd], [], [], 5)

    if len(read_list)==0:
        print 'Type something!'
    else:
        c=term_fd.read(1)
        print 'You typed character', c

# End of script

select.select() is commonly used when you want a process to listen to
two or more sources of input such as pipes or sockets, and data can
become available from either socket at any time.  MUDs and X servers
spend a lot of time in select() loops: wait for input from one of
several sources, handle the input, go back to waiting...

        Andrew Kuchling

        http://starship.skyport.net/crew/amk/



Tue, 25 Apr 2000 03:00:00 GMT  
 Trapping Alarm Out Of Blocking READ

(snip)

Quote:
>    However, a better approach may be the select module, which
>provides an interface to the Unix select() function.  select.select()
>is passed 3 lists containing file objects, and returns 3 different
>lists containing those objects that are ready for reading or writing.
>select.select() has an optional timeout parameter, and will return
>either when an object becomes ready for I/O or when time runs out.
>For example, this opens /dev/tty, and prints "Type something!" every 5
>seconds until you type something.

(snip)

This is a very nice and elegant approach that I hadn't seen before..
Upon further reading of the select module in the Library Reference,
this is definitely a solution to my dilemma.  I did uncover another
solution which comes from setting the MIN and TIME entries in the CC
list when performing my tcsetattr(...).  Originally, I was using MIN=1
and TIME=0, which establishes the blocking read;  changing to MIN=0
and TIME=50 will return when a character is available else after 50
(in this example) tenths of a second (i.e. 5 seconds).

I highly recommend "Linux Programming" from WROX Press as a great book
to have beside the Python docs to understand issues like these.  And I
thank people like Andrew for providing the initial solutions to
investigate.  Thanks.

Ted



Tue, 25 Apr 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. std blocks vs blocks+cache ( was: block behavior)

2. Print outs of Richtext Streams

3. Reading Absolute Blocks/Driver Info

4. Reading blocks of data

5. Macro and struct call-outs from Eiffel

6. non-blocking reads losing data

7. Unable to do non-blocking read on socket

8. Reading the code passed inside a block?

9. reading without process blocking.

10. efficient block reads from a port

11. Read latency of block ram disappears when ram is clocked from bufgce

12. How to use INS OUTS in pentium CPU??

 

 
Powered by phpBB® Forum Software