1 回答

TA贡献1796条经验 获得超10个赞
您不需要线程来执行多个进程 - 只需使用subprocess.Popen- 它不会像 那样阻塞os.system,因此您可以多次运行它并且它们都将并行运行。
如果b是一个包含所有 ip 的列表:
import subprocess
while True:
result = []
for ip in b:
p = subprocess.Popen(['ping', '-n', '1', ip]) # runs ping in background
result.append(p) # store the Popen object for later result retrieval
这将ping在后台运行多个进程!现在你只需要解析结果:
try_again = []
for ip, p in zip(b, result):
if p.wait() == 0:
print(ip, 'is ok!')
else:
print(ip, 'failed!')
try_again.append(ip)
然后,您可以根据需要重复失败的那些:
if not try_again:
break
time.sleep(100)
b = try_again
添加回答
举报