views:

19

answers:

2

so i have a motion sensor connected to an avr micro that is communicating with my python app via usb. im using pyserial to do the comm. during my script i have an infinate loop checking for data from the avr micro. before this loop i start a timer with signal.alarm() that will call a function to end a subprocess. when this alarm goes it interrupts the pyserial comm and the program exits completly. i get the error that pyserial read() is interrupted. is there any way around this issue. any help would be awesome

A: 

The problem is that your alarm will interrupt the read from the serial port, which isn't at all what you want.

It sounds like you probably want to break this into two threads that do work separately.

McPherrinM
No, it's normal to interrupt system calls with signals; it's up to the application or library to retry the call or otherwise handle it sanely. Failing entirely on EINTR is a bug.
Glenn Maynard
thats what im trying now. i have the read() function on a child process and now im running into issues with the child and parent communicatin. this is just from my lack of knowledge of python.
jon
so i learned what pipes are and my problem is solved. thanx everyone who replied
jon
A: 

You are using alarm(), which send a signal, and pyserial, which does reads and writes to a serial port. When you are reading or writing to a device like that, and the SIGALRM signal is received, a read() or a write() call is interrupted so the signal can be handled.

As signals are handled in userspace, and reading and writing is actually handled by the kernel, this makes things rather ugly. This is a know wart of the way signals are handled, and dates back to the very early UNIX days.

Code that handles signals correctly in python may look like:

import errno
while True:
    try:
        data = read_operation()
    except OSError, e:
        if getattr(e, 'errno', errno.EINTR):
            continue
        raise
    else:
        break
Jerub
so ive tried to impliment the error handleing and from debugging im seeing that the error is not being caught and the script still exits. also im very new to using forums, how would i post my code like you did above
jon
To paste code how I did, just paste it into the edit box, then click the 1010 button. It will indent everything by 4 spaces and make it look pretty.
Jerub