如何在for循环每次迭代后睡眠/等待?
我有一个循环,它通过pexpect把配置推送到网络设备上,下面是我脚本的开头:
with open("devices.txt") as ips:
all_ips = [x.rstrip() for x in ips]
for ip in all_ips:
child = pexpect.spawn ('telnet '+ ip)
我该如何让脚本在对我文件 'devices.txt' 中列出的每个设备执行命令后暂停一下呢?
我知道这可以用时间模块来实现,但不太清楚怎么把它放到我的循环里,这样在每次循环结束后就能等待一段时间?
import time
time.sleep(1)
1 个回答
3
在循环中休眠:
import time
with open("devices.txt") as ips:
all_ips = [x.rstrip() for x in ips]
for ip in all_ips:
child = pexpect.spawn ('telnet '+ ip)
time.sleep(1) # sleep here
这个程序在对每个 all_ips
中的 ip
执行 child = pexpect.spawn ('telnet '+ ip)
之后,会暂停1秒钟。