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
22
votes
3 answers

Python: Using popen poll on background process

I am running a long process (actually another python script) in the background. I need to know when it has finished. I have found that Popen.poll() always returns 0 for a background process. Is there another way to do this? p =…
Code Slinger
  • 221
  • 1
  • 2
  • 3
21
votes
3 answers

Alternatives to Python Popen.communicate() memory limitations?

I have the following chunk of Python code (running v2.7) that results in MemoryError exceptions being thrown when I work with large (several GB) files: myProcess = Popen(myCmd, shell=True, stdout=PIPE, stderr=PIPE) myStdout, myStderr =…
Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345
21
votes
4 answers

Send Ctrl-C to remote processes started via subprocess.Popen and ssh

How do I send a Ctrl-C to multiple ssh -t processes in Popen() objects? I have some Python code that kicks off a script on a remote host: # kickoff.py # i call 'ssh' w/ the '-t' flag so that when i press 'ctrl-c', it get's # sent to the script on…
aaronstacy
  • 6,189
  • 13
  • 59
  • 72
21
votes
4 answers

What's the closest I can get to calling a Python function using a different Python version?

Say I have two files: # spam.py import library_Python3_only as l3 def spam(x,y) return l3.bar(x).baz(y) and # beans.py import library_Python2_only as l2 ... Now suppose I wish to call spam from within beans. It's not directly possible since…
leftaroundabout
  • 117,950
  • 5
  • 174
  • 319
21
votes
3 answers

Popen waiting for child process even when the immediate child has terminated

I'm working with Python 2.7 on Windows 8/XP. I have a program A that runs another program B using the following code: p = Popen(["B"], stdout=PIPE, stderr=PIPE) stdout, stderr = p.communicate() return B runs a batch script C. C is a long running…
khattam
  • 1,164
  • 1
  • 8
  • 14
20
votes
5 answers

Reading stdout from a subprocess in real time

Given this code snippet: from subprocess import Popen, PIPE, CalledProcessError def execute(cmd): with Popen(cmd, shell=True, stdout=PIPE, bufsize=1, universal_newlines=True) as p: for line in p.stdout: print(line,…
BPL
  • 9,632
  • 9
  • 59
  • 117
20
votes
1 answer

Using Popen in a thread blocks every incoming Flask-SocketIO request

I have the following situation: I receive a request on a socketio server. I answer it (socket.emit(..)) and then start something with heavy computation load in another thread. If the heavy computation is caused by subprocess.Popen (using…
Schnodderbalken
  • 3,257
  • 4
  • 34
  • 60
20
votes
3 answers

popen equivalent in c++

Is their any C popen() equivalent in C++ ?
Arif
  • 978
  • 12
  • 29
20
votes
2 answers

What is the subprocess.Popen max length of the args parameter?

I am using Popen function from the subprocess module to execute a command line tool: subprocess.Popen(args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None,…
Jesse Vogt
  • 16,229
  • 16
  • 59
  • 72
20
votes
3 answers

Non-blocking pipe using popen?

I'd like to open a pipe using popen() and have non-blocking 'read' access to it. How can I achieve this? (The examples I found were all blocking/synchronous)
jldupont
  • 93,734
  • 56
  • 203
  • 318
20
votes
1 answer

running a process as a different user from Python

I am running a script as userA with root access, from this script I want to make a popen() call and run a different process as userB. os.setuid() does not seem to work for this (unless I am doing this wrong?), and I would like to avoid a linux based…
EEP
  • 715
  • 1
  • 4
  • 17
20
votes
2 answers

python: raise child_exception, OSError: [Errno 2] No such file or directory

I execute a command in python using subprocess.popen() function like the following: omp_cmd = 'cat %s | omp -h %s -u %s -w %s -p %s -X -' %(temp_xml, self.host_IP, self.username, self.password, self.port) xmlResult = Popen(omp_cmd, stdout=PIPE,…
Shang Wang
  • 24,909
  • 20
  • 73
  • 94
19
votes
1 answer

Run a command line with custom environment

In Ruby, I want to be able to: run a command line (via shell) capture both stdout and stderr (preferably as single stream) without using >2&1 (which fails for some commands here) run with additional enviornment variables (without modifying the…
Sridhar Ratnakumar
  • 81,433
  • 63
  • 146
  • 187
19
votes
10 answers

Python subprocess timeout?

Is there any argument or options to setup a timeout for Python's subprocess.Popen method? Something like this: subprocess.Popen(['..'], ..., timeout=20) ?
sultan
  • 5,978
  • 14
  • 59
  • 103
19
votes
5 answers

reading a os.popen(command) into a string

I'm not to sure if my title is right. What I'm doing is writing a python script to automate some of my code writing. So I'm parsing through a .h file. but I want to expand all macros before I start. so I want to do a call to the shell to: gcc -E…
Frames Catherine White
  • 27,368
  • 21
  • 87
  • 137