问题描述
我尝试通过multiprocessing.Process()在test.py中同时调用a.py和b.py,它起作用了.但是a.py,b.py和test.py的进程 CMD 名称是'/usr/bin/python/tmp/test.py'是相同的.
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
我希望这三个进程通过'ps -ef'显示不同的CMD名称,如下所示:(这可以帮助我识别是否正在运行不同的进程.)
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
请帮助咨询:)
源代码如下:
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()
推荐答案
阅读Python»2.7.13文档使用子流程模块
从subprocess
一种 NOWAIT 方法中选择,相应地编辑您的问题代码.
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
使用Python:2.7.9
Tested with Python:2.7.9
这篇关于使用不同的CMD名称同时运行多个python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!