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()
解决方案
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