并行运行多个子进程-python 2.7 [英] Run multiple subprocesses in parallel - python 2.7

查看:168
本文介绍了并行运行多个子进程-python 2.7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

当前状态和问题:

当我运行该文件时;来自elem1(命名空间)的ping将存储在一个名为result.txt的文件中. 但是,我似乎无法使循环回到原来的状态并ping elem2,elem3,... elemN

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

尝试的修复:

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

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.

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()

我的问题:

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

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

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

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

谢谢

推荐答案

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

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.

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

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.

我在这方面没有特殊经验,但是我猜终止问题仅与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/

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/

这篇关于并行运行多个子进程-python 2.7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆