2

I am trying to read the stdout of a simple python script

    import time
    while True:
        print("test")
        time.sleep(1)

with the following lua code

    local p = assert(io.popen("/usr/bin/python test.py", "r"))
    if not p then
        print("error")
    end
    print("process: "..tostring(p))

    while p do
        local line = p:read("*l")
        if line then
            print("line: "..tostring(line))
        end
    end

This prints a process file identifier but no output.

Substituting yes as command shows that the lua code in itself should work. However no method of calling the python script (including shebang) will produce any output. Changing to sys.stdout.write("test" + "\n") doesn't make a difference either.

I tried it on two machines running archlinux and debian. The first one is running python 3.7.2 and lua 5.3.5.

5
  • Could you remove the while loop in the python script and try ? Commented Feb 13, 2019 at 9:49
  • Ok, that does produce output once. Commented Feb 13, 2019 at 9:53
  • It also works in a for loop. Does this mean python buffers the prints until the while loop terminates? Commented Feb 13, 2019 at 10:09
  • 2
    Try, sys.stdout.write("test" + "\n"); sys.stdout.flush() :) Commented Feb 13, 2019 at 10:27
  • Cool, thanks! I was struggling for days with this thinking the problem was with love2d or lua. I also found this answer that shows some other ways. Commented Feb 13, 2019 at 11:03

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.