Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7190745
  • 博文数量: 510
  • 博客积分: 12019
  • 博客等级: 上将
  • 技术积分: 6836
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-01 16:46
文章分类

全部博文(510)

文章存档

2022年(2)

2021年(6)

2020年(59)

2019年(4)

2018年(10)

2017年(5)

2016年(2)

2015年(4)

2014年(4)

2013年(16)

2012年(47)

2011年(65)

2010年(46)

2009年(34)

2008年(52)

2007年(52)

2006年(80)

2005年(22)

分类: Python/Ruby

2012-05-15 11:40:17

外界调用向mgr进程发送消息,mgr根据不同消息启动或关闭相应进程(启动的进程,独占用,只启动一个);
若有进程程退出 则mgr向外界发出信号。

外界代码:

点击(此处)折叠或打开

  1. #!/usr/bin/python
  2. #
  3. # -*- coding:utf-8 -*-
  4. #
  5. #
  6. import sys
  7. import os
  8. import subprocess
  9. import signal
  10. import time
  11. import random

  12. def sig_usr1(signum, frame):
  13.   print("recive sinal from process(exited)")
  14.     
  15. signal.signal(signal.SIGUSR1, sig_usr1)

  16. cmds= ['./mgr.py']
  17. n =1
  18. p = subprocess.Popen( cmds,stdin=subprocess.PIPE,stderr=subprocess.PIPE,close_fds=False)
  19. while True:
  20.     
  21.     time.sleep(1)
  22.     ret = p.poll()
  23.     if ret is None:
  24.       #print("main="+str(n))
  25.       p.stdin.write(str(n)+"\n")
  26.       p.stdin.flush()
  27.       p.send_signal(signal.SIGUSR1)
  28.       n = random.randint(1, 3)
  29.     
  30. print("main exit")

管理进程

点击(此处)折叠或打开

  1. #!/usr/bin/python
  2. #
  3. # -*- coding:utf-8 -*-
  4. #

  5. import sys
  6. import os
  7. import time
  8. import signal
  9. import subprocess

  10. import json

  11. data = {'num': 0, 'error': 0}
  12.   
  13. def getParentID():
  14.   p=subprocess.Popen("ps -ef|grep tt.py|grep -v grep|awk '{print $2}'",stdout=subprocess.PIPE,shell=True)
  15.   ret = int(p.stdout.readline())
  16.   return ret
  17.   
  18. par_id = getParentID()
  19. print(par_id)
  20. exe_process = 0
  21. isRuning = True


  22. def sig_usr1(signum, frame):
  23.   ret = sys.stdin.readline()
  24.   data['num'] = int(ret)
  25.     
  26.     
  27. signal.signal(signal.SIGUSR1, sig_usr1)


  28. while isRuning:
  29.   time.sleep(1)

  30.   print("data['num'] ="+str(data['num']))
  31.   if exe_process == 0: #for open
  32.     if data['num'] == 1:
  33.       exe_process=subprocess.Popen("./a.out",close_fds=False)
  34.     elif data['num'] == 2:
  35.       exe_process=subprocess.Popen("./b.out",close_fds=False)
  36.     else:
  37.       continue
  38.   else: #if exe_process == 0: for close
  39.     ret = exe_process.poll()
  40.     if ret is None:
  41.       if data['num'] == 3:
  42.         exe_process.terminate()
  43.         exe_process = 0
  44.         os.kill(par_id,signal.SIGUSR1)
  45.         
  46.     else: #if ret is None self close
  47.       exe_process = 0
  48.       print("end"+str(par_id))
  49.       os.kill(par_id,signal.SIGUSR1)
  50.       

  51. print "mgr process over "


python进程通讯注意问题
1)发送信号: os.kill(par_id,signal.SIGUSR1)  #注意要 import os
2)subprocess结合管道要注意,不用就不要打开,否则容易出错
    比如p = subprocess.Popen(cmd,stdin=subprocess.PIPE,close_fds=False)
    stdin 要用到。
3)用subprocess调用Popen时候,cmd若是python脚本,要确保能编译通过并运行。则调用,否则不好排除错误
阅读(8230) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~