popen equivalent in c++

You can use the “not yet official” boost.process if you want an object-oriented approach for managing the subprocess. Or you can just use popen itself, if you don’t mind the C-ness of it all.

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

If you’re passing shell=False, then Cmd.exe does not come into play. On windows, subprocess will use the CreateProcess function from Win32 API to create the new process. The documentation for this function states that the second argument (which is build by subprocess.list2cmdline) has a max length of 32,768 characters, including the Unicode terminating null character. … Read more

subprocess.Popen() IO redirect

Altenatively, you can use the stdout parameter with a file object: with open(‘temp.txt’, ‘w’) as output: server = subprocess.Popen(‘./server.py’, stdout=output) server.communicate() As explained in the documentation: stdin, stdout and stderr specify the executed program’s standard input, standard output and standard error file handles, respectively. Valid values are PIPE, an existing file descriptor (a positive integer), … Read more

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

In Linux (or any Unix-y OS), you can mark the underlying file descriptor used by popen() to be non-blocking. #include <fcntl.h> FILE *proc = popen(“tail -f /tmp/test.txt”, “r”); int fd = fileno(proc); int flags; flags = fcntl(fd, F_GETFL, 0); flags |= O_NONBLOCK; fcntl(fd, F_SETFL, flags); If there is no input available, fgets will return NULL … Read more

Why does subprocess.Popen() with shell=True work differently on Linux vs Windows?

Actually on Windows, it does use cmd.exe when shell=True – it prepends cmd.exe /c (it actually looks up the COMSPEC environment variable but defaults to cmd.exe if not present) to the shell arguments. (On Windows 95/98 it uses the intermediate w9xpopen program to actually launch the command). So the strange implementation is actually the UNIX … Read more

Bash style process substitution with Python’s Popen

If pram_axdnull understands “-” convention to mean: “read from stdin” then you could: p = Popen([“pram_axdnull”, str(kmer), input_filename, “-“], stdin=PIPE, stdout=PIPE) output = p.communicate(generate_kmers(3))[0] If the input is generated by external process: kmer_proc = Popen([“generate_kmers”, str(kmer)], stdout=PIPE) p = Popen([“pram_axdnull”, str(kmer), input_filename, “-“], stdin=kmer_proc.stdout, stdout=PIPE) kmer_proc.stdout.close() output = p.communicate()[0] If pram_axdnull doesn’t understand “-” convention: … Read more