Python 为什么我的简单金字塔应用程序不能与pexpect一起工作?
我有一个非常简单的金字塔应用程序,它提供一个简单的静态页面。假设它的名称是Python 为什么我的简单金字塔应用程序不能与pexpect一起工作?,python,pyramid,pexpect,Python,Pyramid,Pexpect,我有一个非常简单的金字塔应用程序,它提供一个简单的静态页面。假设它的名称是mypyramid,并使用端口9999 如果我在另一个linux控制台中手动启动mypyramid,那么我可以使用以下代码打印html字符串 if __name__ == "__main__": import urllib2 print 'trying to download url' response = urllib2.urlopen('http://localhost:9999/index.
mypyramid
,并使用端口9999
如果我在另一个linux控制台中手动启动mypyramid,那么我可以使用以下代码打印html字符串
if __name__ == "__main__":
import urllib2
print 'trying to download url'
response = urllib2.urlopen('http://localhost:9999/index.html')
html = response.read()
print html
但我想在应用程序中自动启动mypyramid 因此,在我的另一个应用程序中,我使用
pexpect
启动mypyramid,然后尝试从http://localhost:9999/index.html
def _start_mypyramid():
p = pexpect.spawn(command='./mypyramid')
return p
if __name__ == "__main__":
p = _start_mypyramid()
print p
print 'mypyramid started'
import urllib2
print 'trying to download url'
response = urllib2.urlopen('http://localhost:9999/index.html')
html = response.read()
print html
似乎mypyramid已使用pexpect
成功启动,因为我可以看到流程的打印,并且已到达mypyramid Start
然而,在尝试下载url
之后,应用程序正在挂起,而我无法获得任何东西
解决办法是什么?我的意思是我认为
pexpect
会创建另一个流程。如果这是真的,那么为什么它会停止对html的检索呢?我的猜测是pexpect.spawn返回的子对象需要通信。
它试图写,但没有人读,所以应用程序停止。(不过我只是在猜测)
如果您没有任何理由使用pexpect(如果不与子进程通信,您可能不会这样做),为什么不直接使用标准模块子进程呢