同时运行许多具有解析论点的Python脚本

发布于 2025-02-09 03:29:05 字数 224 浏览 1 评论 0原文

我有一个带有解析参数的Python脚本“ client_k.py”。为了运行它,我在终端python client_k 1中编写。如果我想在终端同时运行100个客户端,那么是否可以使其与编写python client_k.py 1& python client_k.py 2& ...& python client_k.py 100手工? 提前致谢。

I have a python script with a parse argument 'client_k.py'. To run it I write in the terminal python client_k 1. If I want to run 100 clients at the same time in the terminal, is there a way to make it apart from writing python client_k.py 1 & python client_k.py 2 & ... & python client_k.py 100 by hand?
Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静若繁花 2025-02-16 03:29:05

您可以创建一个新的Python脚本,并多次运行您的脚本。

client_k.py示例:

def client_k(args=None):
    parser = argparse.ArgumentParser()
    parser.add_argument('my_arg')
    args = parser.parse_args(args=args)
    print('My arg:',args.my_arg)

if __name__ == '__main__':
    client_k()

run_client_k.py:

from client_k import client_k

for i in range(100):
    client_k([str(i)])

如果运行它,输出将是:

My arg: 0
My arg: 1
My arg: 2
My arg: 3
...
My arg: 99

或,如果您必须一次执行所有命令,例如在描述中,可以使用子程序:

import subprocess
bashCmd = ''
n = 10 # number of executions
for i in range(n):
    bashCmd +='python client_k.py '+str(i)+' '
    if i != n-1:
        bashCmd +='& ' # Not adding & for the last command

ret = subprocess.run(bashCmd, capture_output=True,shell=True)
print(ret.stdout.decode())

You could create a new python script with a for to run you script several times.

client_k.py example:

def client_k(args=None):
    parser = argparse.ArgumentParser()
    parser.add_argument('my_arg')
    args = parser.parse_args(args=args)
    print('My arg:',args.my_arg)

if __name__ == '__main__':
    client_k()

run_client_k.py:

from client_k import client_k

for i in range(100):
    client_k([str(i)])

If you run it, your output would be:

My arg: 0
My arg: 1
My arg: 2
My arg: 3
...
My arg: 99

Or, if you have to execute all the commands at once such as in your description, you could do it using a subprocess:

import subprocess
bashCmd = ''
n = 10 # number of executions
for i in range(n):
    bashCmd +='python client_k.py '+str(i)+' '
    if i != n-1:
        bashCmd +='& ' # Not adding & for the last command

ret = subprocess.run(bashCmd, capture_output=True,shell=True)
print(ret.stdout.decode())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文