简体   繁体   English

使用不同的CMD名称同时运行多个python脚本

[英]Run multiple python scripts concurrently with different CMD name

I try to call a.py and b.py concurrently in test.py by multiprocessing.Process(), it worked. 我尝试通过multiprocessing.Process()在test.py中同时调用a.py和b.py,它起作用了。 But the process CMD name of a.py, b.py and test.py, which are '/usr/bin/python /tmp/test.py', are the same . 但是a.py,b.py和test.py的进程CMD名称为'/ usr / bin / python /tmp/test.py'是相同的。

# ps -ef | grep b.py
UID   PID  PPID   C STIME   TTY           TIME CMD
501 61486 39878   0  2:33PM ??         0:00.05 /usr/bin/python /tmp/test.py
501 61487 61486   0  2:33PM ??         0:00.01 /usr/bin/python /tmp/test.py
501 61488 61486   0  2:33PM ??         0:00.01 /usr/bin/python /tmp/test.py

I'd like to have these three processes show different CMD names by 'ps -ef' as below: (which can help me to identify whether different process is running or not.) 我希望这三个进程通过“ ps -ef”显示不同的CMD名称,如下所示:(这可以帮助我识别是否正在运行其他进程。)

# ps -ef | grep b.py
UID   PID  PPID   C STIME   TTY           TIME CMD
501 61486 39878   0  2:33PM ??         0:00.05 /usr/bin/python /tmp/test.py
501 61487 61486   0  2:33PM ??         0:00.01 /usr/bin/python /tmp/a.py
501 61488 61486   0  2:33PM ??         0:00.01 /usr/bin/python /tmp/b.py

Please help advice:) 请帮忙咨询:)

Source code is as below: 源代码如下:

test.py: test.py:

import multiprocessing
import a
import b


p1 = multiprocessing.Process(target=a.printa)
p2 = multiprocessing.Process(target=b.printb)

p1.start()
p2.start()

a.py: a.py:

import time


def printa():
    while True:
        print 'a'
        time.sleep(1)

if __name__ == '__main__':
    printa()

b.py: b.py:

import time

def printb():
    while True:
        print 'b'
        time.sleep(1)

if __name__ == '__main__':
    printb()

You are using a.py and b.py as a library in python, therefore it is called under the same name as the executed file test.py . 您正在使用a.pyb.py作为python中的库,因此以与执行文件test.py相同的名称进行调用。 Either using multiprocessing or joblib the same situation occurs. 使用multiprocessingjoblib发生相同的情况。

There is a name option in the Process method ( multiprocessing.Process(self, group=None, target=None, name=None, args=(), kwargs={}) ), but as @fedterzi says, it is only for identification purposes. Process方法中有一个名称选项( multiprocessing.Process(self, group=None, target=None, name=None, args=(), kwargs={}) ),但是正如@fedterzi所说,它仅用于识别目的。

If you want to call a process or file, you can use the library subprocess . 如果要调用进程或文件,则可以使用库进程。

Depending on the task you are performing, such as parellelizing a bunch of processes, you could also use gnu parallel or some other method through bash in order to accomplish your desired behavior. 根据您正在执行的任务,例如将一堆进程并行化,您还可以通过bash使用gnu parallel或其他方法来完成所需的行为。

Read Python » 2.7.13 Documentation using-the-subprocess-module 阅读Python»2.7.13文档, 使用-subprocess模块
Choose from subprocess a NOWAIT Method, edit your Questions code accordingly. subprocess选择一种NOWAIT方法,相应地编辑您的问题代码。

import subprocess

def openCmd(name):
    return subprocess.?

if __name__ == '__main__':
    while True:
        key = raw_input('input 1=open, 0=terminate, q=quit:')
        print(key)
        if key == '1':
            A_p = openCmd(('a'))
            B_p = openCmd(('b'))
        if key == '0':
            A_p.terminate()
            B_p.terminate()
        if key == 'q':
            break

Tested with Python:2.7.9 使用Python测试:2.7.9

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

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