2

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()
5
  • This is a solution to my question, but it seems a little bit odd by listing lines of python xxx.py &. stackoverflow.com/questions/28549641/… Commented Apr 12, 2017 at 7:11
  • 1
    Use Subprocess instead of Process Commented Apr 12, 2017 at 8:15
  • @stovfl Can subprocess call a specific method of a module? Thanks. Commented Apr 14, 2017 at 11:01
  • Yes, simplest case call themethod beneath of if __name__ Commented Apr 14, 2017 at 12:22
  • Could you please help explain a little bit more about the solution? Like an example? Thanks:) Commented Apr 17, 2017 at 10:12

2 Answers 2

0

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 multiprocessingor 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.

Sign up to request clarification or add additional context in comments.

1 Comment

No, the name of the process won't change the process title, it's just there for identification purposes
0

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

2 Comments

What does 'return subprocess.?' mean? Thanks:)
@WillZhou: subprocess.? is a placeholder. Read the suggested Documentation and choose a Method from the subprocess module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.