wexpect
wexpect copied to clipboard
running too slow
When I execute cmd_wrapper.py, I found that the execution speed during the while loop is too slow. Is there any way to improve the execution speed?
from __future__ import print_function
import sys
import os
import re
import time
here = os.path.dirname(os.path.abspath(__file__))
wexpectPath = os.path.dirname(here)
import wexpect
# Path of cmd executable:
cmd_exe = 'cmd'
# The prompt should be more sophisticated than just a '>'.
cmdPrompt = re.compile('[A-Z]\:.+>')
# Start the child process
p = wexpect.spawn(cmd_exe)
# Wait for prompt
p.expect(cmdPrompt, timeout=5)
# print the texts
print(p.before, end='')
print(p.match.group(0), end='')
p.sendline("D:")
p.expect('>', timeout=5)
print('开始执行任务了')
while True:
# Wait and run a command.
command = input("请输入:")
p.sendline(command)
print('开始执行...')
t=time.time()
print(t)
index = p.expect(['>>>', 'bad', wexpect.EOF, wexpect.TIMEOUT], timeout=10)
if index == 0:
print('正常执行')
# print the texts
print(p.before)
print(p.after)
elif index == 1:
# print the texts
print(p.before)
print(p.after)
elif index == 2:
print('出现了错误')
# print the texts
print(p.before)
print(p.after)
elif index == 3:
# print the texts
print('超时了')
print(p.before)
print(p.after)
print('执行结束了...')
print(time.time())
print(time.time()-t)
for example when run python and import datetime,execution time more than five seconds.
Actually, the speed should be very fast under normal execution.
```python
Microsoft Windows [版本 10.0.19042.1706](c) Microsoft Corporation。保留所有权利。
C:\Users\caofei\Desktop\Test_All\pexpect命令的使用>开始执行任务了
请输入:python
开始执行...
1710405615.8823195
正常执行
D:
D:\>python
Python 3.7.9 (tags/v3.7.9:13c94747c7, Aug 17 2020, 18:58:18) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
执行结束了...
1710405622.4716284
6.589308977127075
请输入:import datetime
开始执行...
1710405633.0252686
正常执行
import datetime
>>>
执行结束了...
1710405641.3068957
8.281627178192139
请输入:
I tried this workaround https://github.com/raczben/wexpect/issues/53, and noticed a speedup.