在 mininet 中线程 ping
Threading ping in mininet
我想同时启动两台或多台主机,以便在 mininet 中使用 python ping 其他两台主机,我这样做了,但没有用
def simpleTest(h1,h2):
print (h1.cmd('ping -c5 %s' h2.IP()))
和主要 :
if __name__ == '__main__':
net = Mininet(...)
threads= 3 # three threads
#....codes......
for i in range(1, threads):
hostsrc=net.hosts[i]
hostdest=net.hosts[i+4]
thread = threading.Thread(target=simpleTest(hostsrc,hostdest))
jobs.append(thread)
for j in jobs:
j.start()
for j in jobs:
j.join()
"""
codes ...
"""
请提供任何解决方案...
它通过在这一行中添加 args 来工作...
thread = threading.Thread(target=simpleTest, args=(hostsrc,hostdest,))
问题是,当您将 simpleTest
函数作为参数传递时,您正在调用它。你应该这样写代码:
thread = threading.Thread(target = simpleTest, args = (hostsrc, hostdest,))
或使用 lambda:
thread = threading.Thread(target = lambda:simpleTest(hostsrc, hostdest))
你写的代码把值None传给了target
参数,由于simpleTest
函数returns没有值,所以调用[=时没有任何反应15=]方法。
我想同时启动两台或多台主机,以便在 mininet 中使用 python ping 其他两台主机,我这样做了,但没有用
def simpleTest(h1,h2):
print (h1.cmd('ping -c5 %s' h2.IP()))
和主要 :
if __name__ == '__main__':
net = Mininet(...)
threads= 3 # three threads
#....codes......
for i in range(1, threads):
hostsrc=net.hosts[i]
hostdest=net.hosts[i+4]
thread = threading.Thread(target=simpleTest(hostsrc,hostdest))
jobs.append(thread)
for j in jobs:
j.start()
for j in jobs:
j.join()
"""
codes ...
"""
请提供任何解决方案...
它通过在这一行中添加 args 来工作...
thread = threading.Thread(target=simpleTest, args=(hostsrc,hostdest,))
问题是,当您将 simpleTest
函数作为参数传递时,您正在调用它。你应该这样写代码:
thread = threading.Thread(target = simpleTest, args = (hostsrc, hostdest,))
或使用 lambda:
thread = threading.Thread(target = lambda:simpleTest(hostsrc, hostdest))
你写的代码把值None传给了target
参数,由于simpleTest
函数returns没有值,所以调用[=时没有任何反应15=]方法。