当我从 wexpect运行示例代码spawn
时,它会冻结该方法。
import wexpect
prompt = '[A-Z]\:.+>'
child = wexpect.spawn('cmd.exe')
child.expect(prompt) # Wait for startup prompt
child.sendline('dir') # List the current directory
child.expect(prompt)
print(child.before) # Print the list
child.sendline('exit')
我发现connect_to_child
wexpect 中的方法(下面从存储库复制的代码)会导致问题。它不断抛出“无管道”异常,并陷入永无止境的循环。
def connect_to_child(self):
pipe_name = 'wexpect_{}'.format(self.console_pid)
pipe_full_path = r'\\.\pipe\{}'.format(pipe_name)
logger.debug(f'Trying to connect to pipe: {pipe_full_path}')
while True:
try:
self.pipe = win32file.CreateFile(
pipe_full_path,
win32file.GENERIC_READ | win32file.GENERIC_WRITE,
0,
None,
win32file.OPEN_EXISTING,
0,
None
)
logger.debug('Pipe found')
res = win32pipe.SetNamedPipeHandleState(self.pipe, win32pipe.PIPE_READMODE_MESSAGE,
None, None)
if res == 0:
logger.debug(f"SetNamedPipeHandleState return code: {res}")
return
except pywintypes.error as e:
if e.args[0] == winerror.ERROR_FILE_NOT_FOUND: # 2
logger.debug("no pipe, trying again in a bit later")
time.sleep(0.2)
else:
raise
我正在使用 Windows 10、python 3.9.1 和 wexpect 4.0.0