我遇到了子进程模块的问题,特别是Popen类。我正在学习python中的套接字,并有两个服务器通过套接字进行通信。逻辑如下 服务器A向服务器B发送git目录列表。 服务器B遍历git目录列表并将每个目录的SHA哈希返回给请求服务器A.但是,当出现故障时会出现问题。代码:
Server B : Python version : 2.4.3
while 1:
data=conn.recv(1024)
if not data: break
#DEBUG
#print "data:",data
proc=subprocess.Popen(["git","--git-dir",'/var/git/temp/'+data,"log","-1","--all","--pretty=format:\"%H\""],stdout=subprocess.PIPE)
# Ignore failure and move on
# continue or pass do not seem to work \
# neither does exception handling
if proc.wait() !=0 : pass
sha_hash,err=proc.communicate()
print sha_hash
conn.send(sha_hash)
conn.close()
现在当git项目的默认修订版设置为HEAD等失败时,子进程调用失败并打印返回代码128,但它只是挂起。我无法找到一种方法来返回我对剩余目录的处理,我必须杀死这个过程。我也尝试使用poll()但是没有用。任何建议表示赞赏。
EDIT1:
Removing wait
while 1:
data=conn.recv(1024)
if not data: break
#DEBUG
#print "data:",data
proc=subprocess.Popen(["git","--git-dir",'/var/git/temp/'+data,"log","-1","--all","--pretty=format:\"%H\""],stdout=subprocess.PIPE)
# Ignore failure and move on
# continue or pass do not seem to work \
# neither does exception handling
## removed wait and just using communicate ##
sha_hash,err=proc.communicate()
print sha_hash
conn.send(sha_hash)
conn.close()