#源代码
import subprocess
import os
def ping(host):
result=subprocess.run(
'ping -c2 %s &>/dev/null'%host,shell=True
)
if result.returncode==0:
print('%s:up'%host)
else:
print('%s is not up'%host)
if __name__ == '__main__':
ips = ('176.121.202.%s'%i for i in range(1,255))
for ip in ips:
pid = os.fork()
if not pid:
ping(ip)
exit()
print(time.time())
运行结果
python3 ping.py
1573624962.4055414
176.121.202.1:up
176.121.202.140:is not up
176.121.202.178:up
176.121.202.180:up
176.121.202.181:up
176.121.202.182:up
176.121.202.183:up
相比于上一篇文章的单进程几十分钟运行完,采用多进程的处理方式可以大大缩短程序运行时间,几十分钟瞬间变成几秒钟!