我有一个名为的脚本1st.py,它创建了一个REPL(read-eval-print-loop):
print "Something to print"
while True:
r = raw_input()
if r == 'n':
print "exiting"
break
else:
print "continuing"
Run Code Online (Sandbox Code Playgroud)
然后我1st.py使用以下代码启动:
p = subprocess.Popen(["python","1st.py"], stdin=PIPE, stdout=PIPE)
Run Code Online (Sandbox Code Playgroud)
然后尝试了这个:
print p.communicate()[0]
Run Code Online (Sandbox Code Playgroud)
它失败了,提供了这个追溯:
Traceback (most recent call last):
File "1st.py", line 3, in <module>
r = raw_input()
EOFError: EOF when reading a line
Run Code Online (Sandbox Code Playgroud)
你能解释一下这里发生了什么吗?当我使用时p.stdout.read(),它会永远挂起.
非常具体的问题(我希望):以下三个代码之间有什么区别?
(我希望它只是第一个不等待子进程完成,而第二个和第三个进行.但我需要确定这是唯一的区别......)
我也欢迎其他评论/建议(虽然我已经很清楚shell=True危险和跨平台限制)
请注意,我已经阅读过Python子进程交互,为什么我的进程可以使用Popen.communicate,但不能使用Popen.stdout.read()?并且我不希望/之后需要与程序交互.
另请注意,我已经阅读了Python Popen.communicate()内存限制的替代品?但是我没有真正得到它......
最后,请注意我知道当一个缓冲区使用一种方法填充一个输出时存在死锁的风险,但我在互联网上寻找清晰的解释时迷路了......
第一个代码:
from subprocess import Popen, PIPE
def exe_f(command='ls -l', shell=True):
"""Function to execute a command and return stuff"""
process = Popen(command, shell=shell, stdout=PIPE, stderr=PIPE)
stdout = process.stdout.read()
stderr = process.stderr.read()
return process, stderr, stdout
Run Code Online (Sandbox Code Playgroud)
第二个代码:
from subprocess import Popen, PIPE
from subprocess import communicate
def exe_f(command='ls -l', shell=True):
"""Function to execute a command and return stuff"""
process = Popen(command, shell=shell, stdout=PIPE, stderr=PIPE)
(stdout, stderr) …Run Code Online (Sandbox Code Playgroud) 我有一个帖子,A里面有一个列表.List包含一些对象.现在我想设计一个mechanisim,通过它我可以向线程发送一些消息A.
Thread A循环运行(它不等待或睡眠).其他一些线程,B向线程发送一些消息A,线程A清空所有队列.
如何在线程之间发送消息?
class A extends Thread {
List<Object> objs = something; //Init it
void run() {
while(true) {
//Body which works on objects.
//After receiving an external message, "A" should perform some action, for example, empty objects.
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:我可以这样做吗?
class A extends Thread {
List<Object> objs = something; //Init it
Boolean flag = false;
public void setFlag(boolean value) {
synchronized(flag) {
this.flag = value;
} …Run Code Online (Sandbox Code Playgroud) 我在使用子进程模块获取崩溃程序的输出时遇到问题.我正在使用python2.7和subprocess来调用带有奇怪参数的程序以获得一些段错误为了调用程序,我使用以下代码:
proc = (subprocess.Popen(called,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE))
out,err=proc.communicate()
print out,err
Run Code Online (Sandbox Code Playgroud)
called是一个包含程序名称和参数的列表(一个包含随机字节的字符串,除了子进程根本不喜欢的NULL字节)
当程序没有崩溃时代码表现并向我显示stdout和stderr,但是当它崩溃时,out和err是空的而不是显示着名的"Segmentation fault".
即使程序崩溃,我希望找到一种方法来获取和错误.
希望有人在这里作为一个想法:)
PS:我也尝试过check_output/call/check_call方法
编辑:
我在一个python虚拟环境中的Archlinux 64位上运行这个脚本(这里不应该是重要的东西,但你永远不会知道:p)
segfault发生在我正在尝试运行的C程序中,是缓冲区溢出的结果
问题是当发生段错误时,我无法获得子进程发生的输出
我得到了正确的返回码:-11(SIGSEGV)
使用python我得到:
./dumb2 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
('Exit code was:', -11)
('Output was:', '')
('Errors were:', '')
Run Code Online (Sandbox Code Playgroud)在python外面,我得到:
./dumb2 $(perl -e "print 'A'x50")
BEGINNING OF PROGRAM
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
END OF THE PROGRAM
Segmentation fault (core dumped)
Run Code Online (Sandbox Code Playgroud)shell的返回值是相同的:echo $?返回139所以-11($?&128)
在等待文档(http://docs.python.org/2/library/subprocess.html#subprocess.Popen.wait)中,它说:
警告
当使用stdout = PIPE和/或stderr = PIPE并且子进程为管道生成足够的输出以阻止等待OS管道缓冲区接受更多数据时,这将会死锁.使用communic()来避免这种情况.
由此,我认为communicate可以取代所有使用的wait()如果retcode是不需要.甚至当stdout或stdin没有管,我也可以代替wait()通过communicate().
是对的吗?谢谢!
我是子处理的新手.
我只需要一个非常简单的win32示例,即parent.py和child.py之间的communication().从parent.py发送到child.py的字符串,由child.py更改,并从parent.py发送回parent.py以获取print().
我发布这个是因为我发现的例子最终要么不是win32,要么不使用让我困惑的孩子.
谢谢你的帮助.
我有一个带有嵌入式浏览器(WebBrowser)和一些JavaScript的C#/ WPF应用程序.他们如何在两个方向上相互沟通?使用URL是否切实可行?
JS-> WPF:倾听变化.WPF-> JS:将URL更改为javascript:alert('hello');
有没有更好的办法?
我想用PHP与java通信使用共享数据库进行通信,请帮我提供示例代码或逻辑.
谢谢.
编辑:
我感谢你的建议,是的,我将使用php和java连接到同名数据库.
我面临的问题是我想知道,我是否必须创建将用于通信的通用命令?我正在努力避免脏代码并确保良好的编程技术.
我试图从使用子进程运行的C++程序中的一组打印语句中读出数据.
C++代码:
printf "height= %.15f \\ntilt = %.15f \(%.15f\)\\ncen_volume= %.15f\\nr_volume= %.15f\\n", height, abs(sin(tilt*pi/180)*ring_OR), abs(tilt), c_vol, r_vol; e; //e acts like a print
Run Code Online (Sandbox Code Playgroud)
Python代码:
run = subprocess.call('Name', stdout = subprocess.PIPE, env={'LANG':'C++'})
data, error = run.communicate()
Run Code Online (Sandbox Code Playgroud)
然而,不是获取数据,我得到的只是一个int,退出代码,0或错误代码.当然,python然后告诉我"AttributeError:'int'对象没有属性'communic'".
我如何实际获取数据(printf)?
非常好,我对线程的输出有一点问题,我得到unicode或者我认为并且不让我将它转换为utf-8,这是代码:
import subprocess,sys,time
string = b'dir'
process = subprocess.Popen('cmd.exe', shell=True,cwd="C:\\",stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=None)
process.stdin.write(string)
o,e=process.communicate()
process.wait()
process.stdin.close()
print (o.encode('utf-8'))
Run Code Online (Sandbox Code Playgroud)
我跳转了以下错误:
**Traceback (most recent call last):
File "C:\Documents and Settings\francisco\Escritorio\k.py", line 12, in <module>
print (o.encode(utf-8))
AttributeError: 'bytes' object has no attribute 'encode'**
Run Code Online (Sandbox Code Playgroud)
如果我打印离开打印件,如果你让我:
print(o)
Run Code Online (Sandbox Code Playgroud)
但它打印以下内容:
**b'Microsoft Windows XP [Versi\xa2n 5.1.2600]\r\n(C) Copyright 1985-2001 Microsoft Corp.\r\n\r\nC:\\>\xa8M\xa0s? '**
Run Code Online (Sandbox Code Playgroud)
如果我改变这两行:
string = bytes('dir',encoding="utf-8")
print (n[0].decode("latin"))
Run Code Online (Sandbox Code Playgroud)
我只打印部分输出
失败了吗?
我这样解决了:
process.stdin.write("dir\n".encode())
o,e=process.communicate()
print (o.decode("utf-8"))
Run Code Online (Sandbox Code Playgroud)
但我得到错误:
回溯(最近一次调用最后一次):文件"C:\ Documents and Settings\francisco\Escritorio\k.py",第6行,打印(o.decode("utf-8"))UnicodeDecodeError:'utf-8'编解码器无法解码位置103中的字节0xa3:无效的起始字节
我只是这样打印:
print (o.decode("latin"))
Run Code Online (Sandbox Code Playgroud)
在拉丁语中,我可以更正此错误并将其打印在utf-8中?
communicate ×10
subprocess ×7
python ×6
java ×2
pipe ×2
wait ×2
browser ×1
c# ×1
encode ×1
fuzzing ×1
javascript ×1
php ×1
popen ×1
python-3.x ×1
stdin ×1
stdout ×1
wpf ×1