1

I have the next program written in c:

#include <stdio.h>

int main()
{

while(1)
{

printf("hey\n");

}

return 0;

}

and this program in python

from subprocess import Popen, PIPE

def main():

    proc = Popen("procname.o", stdin=PIPE, stdout=PIPE, shell=True)

    while True:
        print proc.stdout.read()


if __name__ == '__main__':
    main()

yet this line blocks:

proc.stdout.read()

any ideas why? have anyone encountered this before?

DrPrItay
  • 793
  • 6
  • 20
  • Both scripts are doing `while True` without any `time.sleep()` or equivalent. Maybe are you running in stackoverflow ? What do you mean by 'this line blocks' ? Are you getting traceback or something ? – Arount Mar 04 '17 at 12:07
  • 1
    You're executing a `.o` file? Is that right? – Aran-Fey Mar 04 '17 at 12:08
  • Possible duplicate of [Interactive input/output using python](http://stackoverflow.com/questions/19880190/interactive-input-output-using-python) – JuniorCompressor Mar 04 '17 at 12:17
  • Is the subprocess writing anything to stderr? – Luke Woodward Mar 04 '17 at 13:09
  • Around - I mean that process starts sleeping (Blocking state, waiting for an output to be returned by the function read() which would have lead to some syscall ) Rawing - Yes obviously I am :) Luke Woodward - I'll check right now – DrPrItay Mar 04 '17 at 14:01
  • @LukeWoodward nothing either – DrPrItay Mar 04 '17 at 14:07

1 Answers1

-2

You have an infinite while loop, which outputs "hey\n" infinitely. So, the stdout is growing.

If you are on a Unix system, you probably want to add "./" in front of the executable. Like so, "./procname.o".

For example, cexample.c

#include <stdio.h>

int main()
{

printf("hey\n");

return 0;

}

compiled: gcc cexample.c

In the python file, pyexample.py

from subprocess import Popen, PIPE

def main():

proc = Popen("./a.out", stdin=PIPE, stdout=PIPE, shell=True)
print proc.stdout.read()


if __name__ == '__main__':
main()

Run: python pyexample.py

Output:

hey

manvi77
  • 536
  • 1
  • 5
  • 15