Problem with hanging and reading stdout from python in threads
Posted: Mon Feb 18, 2019 6:31 pm
I just posted about a video corruption problem I'm having on the RPi, but I also have a problem reading the output of a python script.
I have some sensors attached to the Raspberry Pi which I'm interfacing with several python scripts. When run on their own they output normally to stdout. When writing to a pipe I already found out I need to flush the buffer or it won't be seen by a plain lua script.
Any of those three will do. However when I open the python program from a love.thread the behavior is very erratic: sometimes it opens normally and passes along the lines from the pipe, and sometimes the thread hangs indefinately without error.
This is the minimal thread code
This either works or it hangs on the io.popen call. I am also using mode2 from the lirc package to capture the raw output of a remote, with this same thread code, and this works very well, no hanging at all.
It mostly seems to appear when I'm using more then one of those threads (different files launching different scripts). And when they hang they hang the whole love game unless I put in a sleep at the start of each thread
I also tried using an intermediate file to write to
This does launch the process every time and it does write to the tmp file correctly (I can tail -f it and the output is piping in) however the tread is not reading any lines from the file!
The most minimal python script I can reproduce this with
Some other thing I noticed, after using print in a thread an io.flush() is needed to make it show up in the love output. Not sure if this is relevant, it doesn't seem to affect the hanging either way.
I've compiled love from the love-xxx-linux-src.tar.gz files in the bitbucket repo with configure and make. I'm getting the same behavoir from 11.0, 11,1 and 11.2.
I'm stuck with this problem for more then a week now, and it's a game for a video art installation that is going to be displayed in a few days, so if anyone is able to help me solve this or work around it that would be greatly appreciated.
I have some sensors attached to the Raspberry Pi which I'm interfacing with several python scripts. When run on their own they output normally to stdout. When writing to a pipe I already found out I need to flush the buffer or it won't be seen by a plain lua script.
Code: Select all
python3 -u script.py
print(str, flush=True)
sys.stdout.write(str)
sys.stdout.flush()
This is the minimal thread code
Code: Select all
print("motion_thread: starting...")
local p = assert(io.popen("python3 /home/pi/zapview/python/motion_detection.py", "r"))
if not p then
print("motion_thread: motion_detection.py error")
end
print("motion_thread: motion_detection.py process: " .. tostring(p))
while p do
local line = p:read("*l")
if line then
print("motion_thread: "..tostring(line))
love.thread.getChannel("motion"):push(true)
end
end
p:close()
It mostly seems to appear when I'm using more then one of those threads (different files launching different scripts). And when they hang they hang the whole love game unless I put in a sleep at the start of each thread
Code: Select all
local function sleep(n)
os.execute("sleep " .. tonumber(n))
end
sleep(5)
Code: Select all
local f = os.tmpname()
printf("motion_thread: using tmp file: "..f)
os.execute("python3 /home/pi/zapview/python/motion_detection.py > "..f)
for line in io.lines(f) do
printf("motion_thread: "..tostring(line))
love.thread.getChannel("motion"):push(true)
end
os.remove(f)
The most minimal python script I can reproduce this with
Code: Select all
#!/usr/bin/env python3
import sys
import time
while True:
# print("test")
sys.stdout.write('test')
sys.stdout.flush()
time.sleep(1)
I've compiled love from the love-xxx-linux-src.tar.gz files in the bitbucket repo with configure and make. I'm getting the same behavoir from 11.0, 11,1 and 11.2.
I'm stuck with this problem for more then a week now, and it's a game for a video art installation that is going to be displayed in a few days, so if anyone is able to help me solve this or work around it that would be greatly appreciated.