1. python3 多线程
# 主线程只负责生成工作线程
# 工作线程只做具体的工作
# 多线程共享进程里的内存块
# 多进程不共享
import threading
def Hello(world, tedu):
print('Hello %s %s!' % (world,tedu))
if __name__ == '__main__':
for i in range(3):
th = threading.Thread(target=Hello, args=('world','tedu')) # 创建工作线程
th.start() # 启动工作线程
2.. python3 多线程ping当前网段主机是否存活
# 多线程的方式ping当前11网段主机
import subprocess
import threading
def ping_host(hosts):
result_ping = subprocess.run("ping %s -c 2 &> /dev/null" % hosts, shell=True)
if result_ping.returncode == 0:
print('%s is up' % hosts)
else:
print('%s is down' % hosts)
if __name__ == '__main__':
iplist = [ '192.168.11.%s' % hosts for hosts in range(1,255)]
for hosts in iplist:
th = threading.Thread(target=ping_host, args=(hosts,))
th.start()