首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pexpect不支持超时

Pexpect不支持超时
EN

Stack Overflow用户
提问于 2012-10-06 22:13:01
回答 2查看 9.1K关注 0票数 0

pexpect不支持超时。

代码语言:javascript
复制
import pexpect
import sys
import os
os.system("""git config --global user.name '#{git_name}'""")
os.system("""git config --global user.email '#{git_email}'""")
child = pexpect.spawn ('git clone https://github.com/davidmontgom/rtbopsConfig.git')
child.logfile = sys.stdout
child.expect ('Username:*')
child.sendline ('#{git_username}')
child.expect ('Pass*')
child.sendline ('#{git_password}')
child.expect(pexpect.EOF, timeout=420)
os.system('cd /home/ubuntu/workspace/rtbopsConfig; git checkout #{branch_name}')


Receiving objects:  16% (1751/10655), 19.36 MiB | 876 KiB/s     KiB/s   
Receiving objects:  16% (1751/10655), 20.64 MiB | 861 KiB/s
103.4.112.50 
103.4.112.50 STDERR: Traceback (most recent call last):
103.4.112.50 
103.4.112.50   File "/tmp/chef-script20121006-19825-5yog10", line 12, in <module>
103.4.112.50 
103.4.112.50     child.expect(pexpect.EOF)
103.4.112.50 
103.4.112.50   File "/usr/local/lib/python2.7/dist-packages/pexpect-2.4-py2.7.egg/pexpect.py", line 1316, in expect
103.4.112.50 
103.4.112.50     return self.expect_list(compiled_pattern_list, timeout, searchwindowsize)
103.4.112.50 
103.4.112.50   File "/usr/local/lib/python2.7/dist-packages/pexpect-2.4-py2.7.egg/pexpect.py", line 1330, in expect_list
103.4.112.50 
103.4.112.50     return self.expect_loop(searcher_re(pattern_list), timeout, searchwindowsize)
103.4.112.50 
103.4.112.50   File "/usr/local/lib/python2.7/dist-packages/pexpect-2.4-py2.7.egg/pexpect.py", line 1414, in expect_loop
103.4.112.50 
103.4.112.50     raise TIMEOUT (str(e) + '\n' + str(self))
103.4.112.50 
103.4.112.50 pexpect.TIMEOUT: Timeout exceeded in read_nonblocking().
103.4.112.50 
103.4.112.50 <pexpect.spawn object at 0x7fbb24704750>
103.4.112.50 
103.4.112.50 version: 2.4 ($Revision: 516 $)
103.4.112.50 
103.4.112.50 command: /usr/bin/git
103.4.112.50 
103.4.112.50 args: ['/usr/bin/git', 'clone', 'https://github.com/davidmontgom/rtbopsConfig.git']
103.4.112.50 
103.4.112.50 searcher: searcher_re:
103.4.112.50 
103.4.112.50     0: EOF
103.4.112.50 
Receiving objects:  16% (1751/10655), 20.64 MiB | 861 KiB/s   873 KiB/s   
103.4.112.50 
Receiving objects:  16% (1751/10655), 20.64 MiB | 861 KiB/s   873 KiB/s   
103.4.112.50 
103.4.112.50 after: <class 'pexpect.TIMEOUT'>

怎样才能让pexpect工作呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-10-06 22:46:38

pexpect会在超时时引发异常(默认在30秒内)。如果不希望出现异常,请将pexpect.TIMEOUT指定为.expect()的模式。

在您的示例中,git clone.expect(pexpect.EOF)上的默认超时后生成pexpect.Timeout异常。尝试:

代码语言:javascript
复制
i = child.expect([EOF, TIMEOUT], timeout=git_clone_timeout)
if i == 1: 
    # handle timeout
票数 4
EN

Stack Overflow用户

发布于 2016-09-16 14:52:55

让超时起作用的另一种方式:

代码语言:javascript
复制
try:
    child.expect([],timeout=300)
except pexpect.EOF:
    #do somthing
except pexpect.TIMEOUT:
    #do somthing
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12760546

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档