popen

Why does pclose return prematurely?

拥有回忆 提交于 2020-07-09 17:13:42
问题 UPDATE 1: This question has been updated to eliminate the multithreading, simplifying its scope. The original problem popen ed in the main thread, and pclose d the child process in a different thread. The problem being asked about is reproducible much more simply, by doing the popen and pclose in the same (main) thread. Update 2: With help from responders at How to check libc version?, I think I've identified that the libc being used is uClibc 0.9.30. The following code popen s a script in

Python - How to call bash commands with pipe?

旧巷老猫 提交于 2020-07-05 01:30:31
问题 I can run this normally on the command line in Linux: $ tar c my_dir | md5sum But when I try to call it with Python I get an error: >>> subprocess.Popen(['tar','-c','my_dir','|','md5sum'],shell=True) <subprocess.Popen object at 0x26c0550> >>> tar: You must specify one of the `-Acdtrux' or `--test-label' options Try `tar --help' or `tar --usage' for more information. 回答1: You have to use subprocess.PIPE , also, to split the command, you should use shlex.split() to prevent strange behaviours in

poll method of subprocess.popen returning None for long process

谁说胖子不能爱 提交于 2020-06-24 12:24:05
问题 I am executing curl command through subprocess. This curl command starts video processing at another server, and waits for the response. Once the process is done, the remote server returns json object. I am checking the status of the subprocess using poll() value, which is None - process not completed, 0- process completed successfully and 1- for error. I am getting the correct response if the processing takes around 30 mins/or less on remote server, but if the processing is taking more time,

Continuous communication between parent and child subprocess in Python (Windows)?

懵懂的女人 提交于 2020-05-29 06:54:30
问题 I have this script: import subprocess p = subprocess.Popen(["myProgram.exe"], stdin=subprocess.PIPE, stdout=subprocess.PIPE) while True: out, _ = p.communicate(input().encode()) print(out.decode()) which works fine until the second input where I get: ValueError: Cannot send input after starting communication Is there a way to have multiple messages sent between the parent and child process in Windows ? [EDIT] I don't have access to the source code of myProgram.exe It is an interactive command

suprocess模块

扶醉桌前 提交于 2020-04-01 06:02:15
suprocess模块 import subprocess ''' sh-3.2# ls /Users/egon/Desktop |grep txt$ mysql.txt tt.txt 事物.txt ''' res1=subprocess.Popen('ls /Users/jieli/Desktop',shell=True,stdout=subprocess.PIPE) res=subprocess.Popen('grep txt$',shell=True,stdin=res1.stdout, stdout=subprocess.PIPE,stderr=subprocess.PIPE) # 此处是否为utf-8要看系统的默认编码,必须输入系统编码格式 print(res.stdout.read().decode('utf-8')) print(res.stderr.read().decode('utf-8')) #等同于上面,但是上面的优势在于,一个数据流可以和另外一个数据流交互,可以通过爬虫得到结果然后交给grep res1=subprocess.Popen('ls /Users/jieli/Desktop |grep txt$',shell=True,stdout=subprocess.PIPE) print(res1.stdout.read().decode('utf-8')

Making python wait until subprocess.call has finished its command

一笑奈何 提交于 2020-03-25 17:52:11
问题 So this question is following on from this (Read comments aswell as that is the path I took). I just want the first call robocopy to finish executing before moving on to the rest of the code. As I want the second robocopy to just skip all of the files as they have already been copied over. However what is happening is that the rest of the script will run (ie starts the second robocopy) while the first robocopy is copying over the files. Below is the code: call(["start", "cmd", "/K", "RoboCopy

执行命令行

与世无争的帅哥 提交于 2020-03-23 21:50:13
1、Linux下使用popen()执行shell命令( http://www.cnblogs.com/caosiyang/archive/2012/06/25/2560976.html ) 简单说一下popen()函数 函数定义 #include <stdio.h> FILE * popen(const char *command , const char *type ); int pclose(FILE *stream); 函数说明   popen()函数通过创建一个管道,调用fork()产生一个子进程,执行一个shell以运行命令来开启一个进程。这个管道必须由pclose()函数关闭,而不是fclose()函数。pclose()函数关闭标准I/O流,等待命令执行结束,然后返回shell的终止状态。如果shell不能被执行,则pclose()返回的终止状态与shell已执行exit一样。   type参数只能是读或者写中的一种,得到的返回值(标准I/O流)也具有和type相应的只读或只写类型。如果type是"r"则文件指针连接到command的标准输出;如果type是"w"则文件指针连接到command的标准输入。   command参数是一个指向以NULL结束的shell命令字符串的指针。这行命令将被传到bin/sh并使用-c标志,shell将执行这个命令。   popen(

Python Popen().stdout.read() hang

牧云@^-^@ 提交于 2020-03-17 08:46:27
问题 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 I run it as a python script and I cannot reproduce this in the python shell. The other script prints just a few words and I am assuming that it's not a buffer issue. Does anyone has idea about what I am doing wrong here? 回答1: You probably want to use

Python Popen().stdout.read() hang

自作多情 提交于 2020-03-17 08:44:25
问题 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 I run it as a python script and I cannot reproduce this in the python shell. The other script prints just a few words and I am assuming that it's not a buffer issue. Does anyone has idea about what I am doing wrong here? 回答1: You probably want to use

C/C++ 程序中调用命令行命令并获取命令行输出结果

爱⌒轻易说出口 提交于 2020-03-16 21:49:54
在 c/c++ 程序中,可以使用 system()函数运行命令行命令,但是只能得到该命令行的 int 型返回值,并不能获得显示结果。例如system(“ls”)只能得到0或非0,如果要获得ls的执行结果,则要通过管道来完成的。首先用popen打开一个命令行的管道,然后通过fgets获得该管道传输的内容,也就是命令行运行的结果。 在linux上运行的例子如下: void executeCMD(const char *cmd, char *result) { char buf_ps[1024]; char ps[1024]={0}; FILE *ptr; strcpy(ps, cmd); if((ptr=popen(ps, "r"))!=NULL) { while(fgets(buf_ps, 1024, ptr)!=NULL) { strcat(result, buf_ps); if(strlen(result)>1024) break; } pclose(ptr); ptr = NULL; } else { printf("popen %s error\n", ps); } } 在这段代码中,参数cmd为要执行的命令行,result为命令行运行结果。输入的cmd命令最好用... 2>&1 的形式,这样将标准错误也读进来。 一个完整的例子是: #include <stdlib.h>