我正在编写一个代码来使用Pexpect自动化测试 所以基本上Telnet进入一个系统并发送命令。 我为它生了一个孩子。我把孩子送去运作。它是通过引用传递还是通过值传递?
def test(child,clicommand,min,max,message):
child.sendline("xyz")
"""Basically I am sending only sendline But I need 2 more expects.
Because of two execute called before this method.
Both the expects are not blocking and the execute"""
child.expect("() >")
child.expect("() >")
child.expect("() >")
return
def execute(child,clicommand):
print "Executing CLI command: "+clicommand
child.sendline(clicommand)
child.expect("() >")
return
child = pexpect.spawn("telnet xx.xx.xx.xx xxxx")
child.sendline("")
child.expect("() >")
execute(child,"abc")
execute(child,"abc")
test(child,"xyz",1,2,"Error")
预期是缓冲还是排队?我怎样才能使它异步? 我甚至试过尝试除了,但没有抛出异常
答案 0 :(得分:0)
如上所述here,您无法使用相同的进程执行异步输入/输出,因此这样的锁定算法可能对您有用:
from threading import Lock
lock=Lock()
def send_command(str, int): #int is the repetition number
with lock:
child.sendline(str)
for x in range(int):
child.expect("() >")
然后这样称呼:
def test():
send_command("xyz",3)
希望它有所帮助。