cmd运行python脚本处理其他文件_使用不同的CMD名称同时运行多个python脚本

1586010002-jmsa.png

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值