[英]pexpect sendline is too slow
I'm running a subprocess using pexpect.我正在使用 pexpect 运行一个子进程。 I'm communicating with it off and on via sendline and readline.
我通过 sendline 和 readline 断断续续地与它通信。 cProfile indicates my program spends most of its time waiting around for sendline to finish, which is not desired behavior.
cProfile 表明我的程序大部分时间都在等待发送线完成,这不是我们想要的行为。 Python pseudocode:
Python伪代码:
def reset(self):
self.proc = pexpect.spawn("node ./my_lovely_program.js")
...
def step(self, info):
self.proc.sendline(info)
while (my_condition):
response = self.proc.readline().decode()
# evaluate my_condition
return retval
Meanwhile, in the style of Waiting for user to enter input in Node.js , buried well into my node application is:同时,在等待用户在 Node.js 中输入输入的风格中,我的节点应用程序很好地隐藏了:
let ans = await this.askQuestion("");
// do_stuff(ans);
This gets run by two different instances of the relevant class at the same time.这同时由相关类的两个不同实例运行。 This is all well and good and runs satisfactorily.
这一切都很好,运行令人满意。 Then I run
然后我跑
python3 -m cProfile -s cumtime main.py
And get this:得到这个:
ncalls tottime percall cumtime percall filename:lineno(function)
498/1 0.002 0.000 262.820 262.820 {built-in method builtins.exec}
1 0.107 0.107 262.820 262.820 main loop
1 0.099 0.099 262.081 262.081 also main loop
3774 0.022 0.000 214.272 0.057 main.py:24(step)
33596 203.977 0.006 203.977 0.006 {built-in method time.sleep}
3774 0.017 0.000 189.138 0.050 pty_spawn.py:570(sendline)
3774 0.049 0.000 189.105 0.050 pty_spawn.py:526(send)
3875 0.158 0.000 53.767 0.014 main.py:28(scrape_input)
14756 0.052 0.000 53.434 0.004 spawnbase.py:459(readline)
14756 0.056 0.000 53.382 0.004 spawnbase.py:240(expect)
14756 0.072 0.000 52.985 0.004 spawnbase.py:343(expect_list)
14756 0.242 0.000 52.848 0.004 expect.py:91(expect_loop)
29721 0.235 0.000 47.113 0.002 pty_spawn.py:415(read_nonblocking)
69657 0.113 0.000 46.250 0.001 pty_spawn.py:448(select)
69657 0.103 0.000 46.137 0.001 utils.py:130(select_ignore_interrupts)
69657 46.016 0.001 46.016 0.001 {built-in method select.select}
...
Between them, readline and especially sendline take up over 90% of the programs runtime.其中,readline,尤其是sendline,占了程序运行时间的90%以上。 Most of that time is spent sleeping.
大部分时间都在睡觉。 This is very sad and I want to fix it.
这是非常可悲的,我想解决它。 I'm sending 20-30 characters each time.
我每次发送 20-30 个字符。 I've tried reading from stdin in other ways (eg https://www.dev2qa.com/node-js-get-user-input-from-command-line-prompt-example/ ) and am ready to keep going down that route if that's the way to go.
我尝试以其他方式从 stdin 读取(例如https://www.dev2qa.com/node-js-get-user-input-from-command-line-prompt-example/ )并准备继续下去这条路,如果那是要走的路。 (The issue I am having there is a separate one; on the second communication, the input gets read twice).
(我遇到的问题是一个单独的问题;在第二次通信中,输入被读取两次)。 Posix_spawn performance when capturing process output used popen;
使用 popen 捕获进程输出时的 Posix_spawn 性能; I remember using that several months ago and it deadlocking.
我记得几个月前使用它并且它陷入僵局。
pexpect has a default 50ms wait time for send(). pexpect 的 send() 等待时间默认为 50 毫秒。 You can disable this by setting:
您可以通过设置禁用此功能:
proc.delaybeforesend = None
See the documentation for an explanation of why they put this delay in.请参阅文档以解释他们为什么要延迟。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.