Questions tagged [popen]

popen() is a way to communicate with subprocesses using a file-like interface. It originated in C, but has been ported to other languages (via extensions) such as Python.

FILE *popen(const char *command, const char *type)

popen() (Process OPEN) is a method by which programs can start and communicate with other programs using a file-like interface. This funcion is not mandated by ANSI, but is specified by POSIX.

popen() allows the programmer to avoid the internal workings of fork() and pipe() (this is how it is implemented on UNIX-like systems) by presenting a file object. This allows for the use of functions such as fprintf() and fscanf(), presenting a more orthogonal interface to the programmer (excepting closing the process - the programmer must use pclose() to stop a subprocess).

This function has been ported to a number of programming languages, such as Python (os.popen), Ruby (IO.popen), Tcl (open |command), etc.

2441 questions
15
votes
2 answers

How to clean up after subprocess.Popen?

I have a long-running python script with a perl worker subprocess. Data is sent in and out of the child proc through its stdin and stdout. Periodically, the child must be restarted. Unfortunately, after a while of running, it runs out of files…
dkuebric
  • 415
  • 1
  • 3
  • 6
15
votes
5 answers

Very large input and piping using subprocess.Popen

I have pretty simple problem. I have a large file that goes through three steps, a decoding step using an external program, some processing in python, and then recoding using another external program. I have been using subprocess.Popen() to try to…
seandavi
  • 2,818
  • 4
  • 25
  • 52
15
votes
2 answers

Can I pipe a io.BytesIO() stream to subprocess.popen() in Python?

I'm trying to pipe a io.BytesIO() bytetream to a separate program using subprocess.popen(), but I don't know how or if this is at all possible. Documentation and examples are all about text and newlines. When I whip up something like this: import…
Redsandro
  • 11,060
  • 13
  • 76
  • 106
15
votes
5 answers

How to prevent fgets blocks when file stream has no new data

I have a popen() function which executes tail -f sometextfile. Aslong as there is data in the filestream obviously I can get the data through fgets(). Now, if no new data comes from tail, fgets() hangs. I tried ferror() and feof() to no avail. How…
SinisterDex
  • 811
  • 2
  • 10
  • 13
15
votes
3 answers

Python Popen().stdout.read() hang

I'm trying to get output of another script, using Python's subprocess.Popen like follows process = Popen(command, stdout=PIPE, shell=True) exitcode = process.wait() output = process.stdout.read() # hangs here It hangs at the third line, only when…
lyomi
  • 4,230
  • 6
  • 30
  • 39
15
votes
1 answer

Why does shell=True eat my subprocess.Popen stdout?

It seems that using shell=True in the first process of a chain somehow drops the stdout from downstream tasks: p1 = Popen(['echo','hello'], stdout=PIPE) p2 = Popen('cat', stdin=p1.stdout, stdout=PIPE) p2.communicate() # outputs correctly ('hello\n',…
Jake Biesinger
  • 5,538
  • 2
  • 23
  • 25
15
votes
2 answers

Reading/writing to a Popen() subprocess

I'm trying to talk to a child process using the python subprocess.Popen() call. In my real code, I'm implementing a type of IPC, so I want to write some data, read the response, write some more data, read the response, and so on. Because of this, I…
Mats Ekberg
  • 1,695
  • 4
  • 15
  • 23
14
votes
2 answers

Keep a subprocess alive and keep giving it commands? Python

If I spawn a new subprocess in python with a given command (let's say I start the python interpreter with the python command), how can I send new data to the process (via STDIN)?
user623990
14
votes
2 answers

subprocess.Popen() IO redirect

Trying to redirect a subprocess' output to a file. server.py: while 1: print "Count " + str(count) sys.stdout.flush() count = count + 1 time.sleep(1) Laucher: cmd = './server.py >temp.txt' args = shlex.split(cmd) server =…
Pete Roberts
  • 143
  • 1
  • 1
  • 5
14
votes
2 answers

How to programmatically count the number of files in an archive using python

In the program I maintain it is done as in: # count the files in the archive length = 0 command = ur'"%s" l -slt "%s"' % (u'path/to/7z.exe', srcFile) ins, err = Popen(command, stdout=PIPE, stdin=PIPE, …
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
13
votes
6 answers

Portable way to find out if a command exists (C/C++)

C standard library provides functions system and popen to run a command. But is there a portable way to detect if a command exists?
Sherwood
13
votes
4 answers

popen() alternative

My question is extension of this one: popen creates an extra sh process Motives: 1) My program need to create a child which does tail on a file. I need to process the output line by line. That is why I am using popen because it returns FILE *. I can…
hari
  • 9,439
  • 27
  • 76
  • 110
13
votes
1 answer

How to avoid the deadlock in a subprocess without using communicate()

proc = subprocess.Popen(['start'],stdin=subprocess.PIPE,stdout=subprocess.PIPE) proc.stdin.write('issue commands') proc.stdin.write('issue more commands') output = proc.stdout.read() # Deadlocked here # Actually I have more commands to issue here…
JAugust
  • 557
  • 1
  • 5
  • 14
13
votes
1 answer

Python Popen - wait vs communicate vs CalledProcessError

Continuing from my previous question I see that to get the error code of a process I spawned via Popen in python I have to call either wait() or communicate() (which can be used to access the Popen stdout and stderr attributes): app7z =…
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
13
votes
2 answers

Getting the PID from popen

I have a program that uses popen() in order to open and read the output from a shell command. The problem is, as far as I can tell, there is no easy way to get the PID of the running process, and hence, you can't kill it if it gets stuck. So the…
Gillespie
  • 5,780
  • 3
  • 32
  • 54