简体   繁体   English

使用 N 个参数运行 subprocess.popen

[英]Run subprocess.popen with N arguments

subprocess.popen invokes process in background. subprocess.popen 在后台调用进程。

I am invoking php process and sometimes python process, and every time arguments may be different.我正在调用 php 进程,有时是 python 进程,每次参数可能不同。 I want to write one function and in that I will pass list of arguments.我想编写一个函数,并在其中传递参数列表。 And that function will execute process based on the arguments.该函数将根据参数执行进程。

So what I am doing is, written below and it was bad way actually and I don't want to write so many lines.所以我正在做的是,写在下面,实际上这是糟糕的方式,我不想写这么多行。

    def RunBackGroundProcess(args):
        # args is an array of params
        # index 0 - command - php/python or anyother
        # index 1 - filename - that you want to run. Specify fullpath if required
        # index 2 - param_1
        # index 3 - param_2
        # index N - param_N

        if(len(args) == 2):
            subprocess.Popen([args[0], args[1], args[2]])

        if(len(args) == 3):
            subprocess.Popen([args[0], args[1], args[2], args[3]])

        if(len(args) == 4):
            subprocess.Popen([args[0], args[1], args[2], args[3], args[4]])

What about if I have 10 arguments or 15 arguments?如果我有 10 个参数或 15 个参数呢? This is bad way, right?这是不好的方式,对吧? Help me to write this code in smaller lines.帮我把这段代码写成更小的行。

you are passing args as list, So while subprocess.Popen() no need to check argument length you can just pass as it is to subprocess您将 args 作为列表传递,因此虽然 subprocess.Popen() 无需检查参数长度,但您可以按原样传递给 subprocess

def RunBackGroundProcess(args):
    subprocess.Popen(args)

This will work这将工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM