I'm experimenting with, or better trying to learn and understand, the subprocess modules using Python on a Raspberry Pi running Raspbain Jessie. In the code below I'm piping the Internet traffic for an Internet appliance to stdout. The records in the pipe can come anytime, from seconds apart to minutes or even hours apart. For the way I have it coded now, if there is nothing in the pipe the program just stops and waits. That is ok, if all is good with the world. But from experience I know that my program might hang or the Pi might hang or ...
On my Pi I have the PiMode's UPS HV3.0A card, which, besides being a self powered UPS, it has "watchdog" like function which will restart the Pi if its counter (independent of the Pi' processor and power supply) counts down to zero. My idea is to experiment with this function such that
- if a record can be read then reset the counter
- if there is no record to be read, ie the pipe read is blocked, also reset the timer and "continue" the loop.
If neither of those conditions are met, then I assume the program or the system has hung and the HV3.0A card will restart the Pi and related programs when the timer reaches zero.
To get started, I have created the program below. I would like to know what code I need to determine if the pipe has no data, ie blocked and the best way to code the I2CSET command.
#!/usr/bin/env python import subprocess nul_f = open('/dev/null', 'w') try: tcpflow = subprocess.Popen(['/usr/bin/sudo /usr/local/bin/tcpflow -C -0 -i eth1 -s -o /mnt/usbdrive/TCPFLOW/ tcp port 80'], stdout=subprocess.PIPE, shell=True, stderr=nul_f,) grep = subprocess.Popen(['stdbuf -oL egrep "GET"'], shell=True, stdin=tcpflow.stdout, stdout=subprocess.PIPE, stderr=nul_f) print 'Listener running' i=0 while i<10: i=i+1 if (code here to tell me if the pipe is blocked, empty): code here to simulate this CL command: sudo i2cset -y 1 0x6b 0x05 0x0f continue line = grep.stdout.readline() # ok the read worked now set the counter anyway code here to simulate this CL command: sudo i2cset -y 1 0x6b 0x05 0x0f print '\t --', i, line except KeyboardInterrupt: print 'done' tcpflow.terminate() tcpflow.kill() grep.terminate() grep.kill() nul_f.close()
In the code above I have indicated two items which I'm not clear on:
- code to tell me if the pipe is blocked, empty
- code to simulate the sudo i2cset -y 1 0x6b 0x05 0x0 command line command. I would initially have thought subproccess.call(['i2cset','-y', ' 1', ' 0x6b', ' 0x05', ' 0x0f]) would work. However it was also suggested that I use the smbus routines. I'm currently investigating these options. Do you have any recommendations?
Thus, if you can offer any help/suggestions/references on how to achieve my plan I would appreciate it. Thanks...RDK
Administator...If this question is in the wrong place, please feel free to move it or indicate to me where and I will repost it...