免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3789 | 回复: 5

Python: 关于脚本运行10分钟后退出的问题 [复制链接]

论坛徽章:
0
发表于 2016-04-27 15:43 |显示全部楼层
我写了一个测试脚本,运行10分钟后测试结束并退出,但是事假运行结果并没有按时结束等,想请帮忙看看脚本哪里有问题? 谢谢

import ldap
import os
import sys
import time
import threading
from subprocess import *

start=time.clock()
end=time.clock ()

i = 0
while True:
  i = i + 1

  class mythread(threading.Thread):
    def _init_(self,num):
        threading.Thread.__init__(self)
        self.num = num
        def run(self):
                self.num
                print threading.currentThread().getName()

  #time.sleep(1)
  DIR=sys.argv[1]
  os.environ['ORACLE_HOME'] = "%s/Middleware/Oracle_IDM" % DIR
  print(os.environ['ORACLE_HOME'])

  from commands import getoutput
  result = getoutput( '%s/Middleware/Oracle_IDM/bin/ldapsearch -a always -D cn=orcladmin -w welcome1 -p 3060 -b "" -s sub "objectclass=*" orcldirectoryversion ou=Groups Privilege, cn=Operatio                            nURLs,cn=DAS,cn=Products,cn=OracleContext,dc=cn,dc=oracle,dc=com' % DIR)
  print result

  print i

if int(end-start)==1:
   print('Script Exit')

论坛徽章:
4
程序设计版块每日发帖之星
日期:2015-10-14 06:20:00每日论坛发贴之星
日期:2015-10-14 06:20:00程序设计版块每日发帖之星
日期:2016-05-02 06:20:00程序设计版块每日发帖之星
日期:2016-05-08 06:20:00
发表于 2016-04-28 10:50 |显示全部楼层

论坛徽章:
0
发表于 2016-05-03 16:45 |显示全部楼层
本帖最后由 cgiyan 于 2016-05-05 10:12 编辑

我修改了我的测试脚本,但是运行报错,麻烦高手给看看,谢谢!
  1. [code]#!/usr/bin/env python
  2. #coding=utf-8
  3. #vim: tabstop=4 shiftwidth=4 softtabstop=4

  4. import os
  5. import sys
  6. import time
  7. import threading
  8. import shlex
  9. import signal
  10. from subprocess import Popen, PIPE

  11. i = 0
  12. while True:
  13.   i = i + 1

  14.   class mythread(threading.Thread):
  15.     def _init_(self,num):
  16.         threading.Thread.__init__(self)
  17.         self.num = num
  18.         def run(self):
  19.                 self.num
  20.                 print threading.currentThread().getName()

  21.   DIR=sys.argv[1]
  22.   os.environ['ORACLE_HOME'] = "%s/Middleware/Oracle_IDM" % DIR
  23.   print(os.environ['ORACLE_HOME'])

  24.   from commands import getoutput
  25.   result = getoutput( '%s/Middleware/Oracle_IDM/bin/ldapsearch -a always -D cn=orcladmin -w welcome1 -p 3060 -b "" -s sub "objectclass=*" orcldirectoryversion ou=Groups Privilege, cn=OperationURLs,
  26.                       cn=DAS,cn=Products,cn=OracleContext,dc=cn,dc=oracle,dc=com' % DIR)
  27.   print result

  28.   print i

  29. def cmd_run(cmd, timeout=60):
  30.     t_beginning = int(time.time())

  31.     cmdargs = shlex.split(cmd)
  32.     p = Popen(cmdargs, stdout=PIPE, stderr=PIPE, preexec_fn=os.setsid)

  33.     if p.poll() is not None:
  34.         return

  35.     if int(time.time()) - t_beginning >= timeout:
  36.         os.killpg(p.pid, signal.SIGTERM)
  37.         raise Exception(cmd, timeout)

  38.     return p.returncode

  39. if __name__ == "__main__":
  40.     t_beginning = int(time.time())
  41.     try:
  42.         while True:
  43.             cmd_run(sys.argv[1])
  44.             if int(time.time()) - t_beginning >= 10:
  45.                 break
  46.     except IndexError as e:
  47.         print e

  48. 运行报错:

  49. python ldap2.py /u03
  50.   File "ldap2.py", line 57
  51.     except IndexError as e:
  52.                        ^
  53. SyntaxError: invalid syntax
复制代码
[/code]



论坛徽章:
5
巨蟹座
日期:2014-08-28 18:12:342015年迎新春徽章
日期:2015-03-04 10:01:4415-16赛季CBA联赛之江苏
日期:2016-04-28 09:43:3115-16赛季CBA联赛之吉林
日期:2016-06-22 10:34:4315-16赛季CBA联赛之山西
日期:2016-08-16 16:29:55
发表于 2016-05-04 15:18 |显示全部楼层
回复 3# cgiyan


    至少你的code 排版 要排好 不然看的好累。。。

论坛徽章:
2
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:57:09
发表于 2016-05-07 12:55 |显示全部楼层
本帖最后由 lolizeppelin 于 2016-05-07 12:57 编辑

写法错误

应该写一个循环  检查p.poll()是否为none
然后给这个循环每次都检查当前时间差是否超过10分钟

例如
start_time = time.time()
while 1:
   if p.poll() is not None:  # 子进程已经完结
       break
  if time.time() - start_time >= 10分钟:
      p.kill()
      break
  time.sleep(0.001)

论坛徽章:
0
发表于 2016-06-02 14:54 |显示全部楼层
谢谢指点,我试试。回复 5# lolizeppelin


   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP