并行运行多个子进程 - python 2.7

时间:2022-10-26 21:35:05

The script shown below is my attempt at pinging multiple network namespaces using python 2.7 running on Linux (Fedora) OS.

下面显示的脚本是我尝试使用在Linux(Fedora)操作系统上运行的python 2.7来ping多个网络命名空间。

Current status and problem:

现状和问题:

When I run this file; the ping from elem1 (namespace) gets stored in a file called results.txt. But, I can't seem to get the loop to come back around and ping elem2, elem3, ... elemN

当我运行这个文件; elem1(命名空间)中的ping存储在名为results.txt的文件中。但是,我似乎无法让循环回来并ping elem2,elem3,... elemN

Attempted Fixes:

I tried killing the process using "kill -9 p.pid" (as shown) in the hope that this would kill the process and then a new process could be created on next iteration of the loop. However this is not the case! I've checked the documentation ( https://docs.python.org/2/library/subprocess.html ) and tried several different permutations of kill(), terminate(), removing shell=True etc... but to no avail.

我尝试使用“kill -9 p.pid”(如图所示)杀死进程,希望这会终止进程,然后在循环的下一次迭代中创建一个新进程。然而,这种情况并非如此!我检查了文档(https://docs.python.org/2/library/subprocess.html)并尝试了几种不同的kill(),terminate(),删除shell = True等等的排列...但是没有无济于事。

import time
import os
import signal
import subprocess    

IP_ADDR="192.168.1.1"    

def main():
arry =["elem1", "elem2", "elem3", "elem4", "elem5", "elem6", "elem7"]  array of network namespaces's to ping

        with open('results.txt', 'a+') as outfile:
            for elem in arry:
                command = "ip netns exec {0} ping {1}".format(elem, IP_ADDR)
                outfile.write("\n\nPinging {}\n".format(elem))
                p = subprocess.Popen(command, shell=True, stdout=outfile)
                command_kill = "kill -9 {}".format(p.pid)
                time.sleep(2) #wait 5 seconds
                p.kill()
        outfile.close()

if __name__ == '__main__':
    main()

My Questions:

(1) Can someone explain what the code is doing here?

(1)有人可以解释代码在这里做了什么吗?

(2) Can you suggest a means to achieve my aforementioned goal?

(2)你能否提出实现上述目标的方法?

Thank you

1 个解决方案

#1


0  

Along with the modifications mentioned by @chepner, you can try to use subprocess.call() instead of subprocess.Popen(). The latter method is not blocking and this causes all the commands to be executed simultaneously. However, call() is blocking and therefore your script will wait until the pinging is finished before entering next iteration of the loop. This will cause the output of your commands to be in sequential order and not interleaving.

除了@chepner提到的修改之外,您还可以尝试使用subprocess.call()而不是subprocess.Popen()。后一种方法没有阻塞,这导致所有命令同时执行。但是,call()是阻塞的,因此您的脚本将等到ping完成后再进入循环的下一次迭代。这将导致命令的输出按顺序排列而不是交错。

If you need to execute the commands in parallel, I would suggest to write the outputs into different files and combine them after all commands are finished.

如果你需要并行执行命令,我建议将输出写入不同的文件,并在所有命令完成后将它们组合起来。

Edit: I have no particular experience in this area, but I guess the termination issue is related to the ping command only. Check the manual page here: https://linux.die.net/man/8/ping. In our case, we need to ping the destination X times. This is specified by using parameter -c X, where X defines the number of packets to be sent. This parameter can be also combined with parameter -w / -W which specify the timeout limit for the ping command. Take a look at some examples: https://www.thegeekstuff.com/2009/11/ping-tutorial-13-effective-ping-command-examples/

编辑:我在这方面没有特别的经验,但我猜终止问题只与ping命令有关。请查看手册页:https://linux.die.net/man/8/ping。在我们的例子中,我们需要ping目的地X次。这是通过使用参数-c X指定的,其中X定义要发送的数据包的数量。此参数还可以与参数-w / -W结合使用,后者指定ping命令的超时限制。看看一些例子:https://www.thegeekstuff.com/2009/11/ping-tutorial-13-effective-ping-command-examples/

#1


0  

Along with the modifications mentioned by @chepner, you can try to use subprocess.call() instead of subprocess.Popen(). The latter method is not blocking and this causes all the commands to be executed simultaneously. However, call() is blocking and therefore your script will wait until the pinging is finished before entering next iteration of the loop. This will cause the output of your commands to be in sequential order and not interleaving.

除了@chepner提到的修改之外,您还可以尝试使用subprocess.call()而不是subprocess.Popen()。后一种方法没有阻塞,这导致所有命令同时执行。但是,call()是阻塞的,因此您的脚本将等到ping完成后再进入循环的下一次迭代。这将导致命令的输出按顺序排列而不是交错。

If you need to execute the commands in parallel, I would suggest to write the outputs into different files and combine them after all commands are finished.

如果你需要并行执行命令,我建议将输出写入不同的文件,并在所有命令完成后将它们组合起来。

Edit: I have no particular experience in this area, but I guess the termination issue is related to the ping command only. Check the manual page here: https://linux.die.net/man/8/ping. In our case, we need to ping the destination X times. This is specified by using parameter -c X, where X defines the number of packets to be sent. This parameter can be also combined with parameter -w / -W which specify the timeout limit for the ping command. Take a look at some examples: https://www.thegeekstuff.com/2009/11/ping-tutorial-13-effective-ping-command-examples/

编辑:我在这方面没有特别的经验,但我猜终止问题只与ping命令有关。请查看手册页:https://linux.die.net/man/8/ping。在我们的例子中,我们需要ping目的地X次。这是通过使用参数-c X指定的,其中X定义要发送的数据包的数量。此参数还可以与参数-w / -W结合使用,后者指定ping命令的超时限制。看看一些例子:https://www.thegeekstuff.com/2009/11/ping-tutorial-13-effective-ping-command-examples/