简体   繁体   中英

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. But the process CMD name of a.py, b.py and test.py, which are '/usr/bin/python /tmp/test.py', are the same .

# 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 | 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:

import multiprocessing
import a
import b


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

p1.start()
p2.start()

a.py:

import time


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

if __name__ == '__main__':
    printa()

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 . Either using multiprocessing or joblib the same situation occurs.

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.

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.

Read Python » 2.7.13 Documentation using-the-subprocess-module
Choose from subprocess a NOWAIT Method, edit your Questions code accordingly.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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