minin中的线程ping

2024-09-30 08:24:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我想同时启动两个或多个主机,用python在mininet中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 ...
    """

有什么解决办法吗。。。你知道吗


Tags: infornetjobsh2pingh1thread
2条回答

它通过在这行中添加参数来工作。。。你知道吗

        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函数不返回值,所以调用start方法时没有发生任何事情。你知道吗

相关问题 更多 >

    热门问题