Chinaunix首页 | 论坛 | 博客
  • 博客访问: 466555
  • 博文数量: 65
  • 博客积分: 2645
  • 博客等级: 少校
  • 技术积分: 675
  • 用 户 组: 普通用户
  • 注册时间: 2006-01-08 17:04
文章分类

全部博文(65)

文章存档

2010年(5)

2009年(5)

2008年(14)

2007年(35)

2006年(6)

分类: Python/Ruby

2008-01-29 13:03:09

前几天调程序时,程序出错.手工停止后,表被锁了. 我写了一个脚本, 查看数据库多久之后将挂掉的 session 收集掉

#!/usr/bin/env python
#coding=GBK
#!/usr/bin/env python
#11:22
import time
import cx_Oracle


class BindVarTypeError(Exception):pass

db_tns = 'username/password@asdf'

def get_sysdate():
    return time.strftime("%Y%m%d%H%M%S", time.localtime())


def DB_Query(func):
    def result(*args, **dic):
        orcl = cx_Oracle.connect(db_tns)
        curs = orcl.cursor()
        try:
            sql_str = func(curs, **dic)

            tuple_ans = curs.fetchall()
        finally:
            curs.close()
            orcl.close()
        return tuple_ans

    return result


@DB_Query
def _qry_sql_(curs=None, sqlstr ="", bindVars=None):
    if isinstance(bindVars, dict):
        curs.execute(sqlstr, bindVars)
    elif isinstance(bindVars, list):
        curs.executemany(sqlstr, bindVars)
    elif bindVars is None:
        curs.execute(sqlstr)
    else:
        raise BindVarTypeError

def qry_sql(sqlstr, bindVars=None):
    return _qry_sql_(sqlstr=sqlstr,bindVars=bindVars)

def cost_time(func):
    from time import time
    def result(*args,**dic):
        beign=time()
        ans = func(*args,**dic)
        print "cost time : ",time()-beign
        return ans
    return result

@cost_time
def test():
    sql="select count(*) from v$session a where a.sid = '100'"
    while True:
        iCount =qry_sql(sql)[0][0]
        if iCount <=0 :
            break
        else:
            print get_sysdate(), "还在"
            time.sleep(5)
    else:
        print get_sysdate(),"结束了."
       


if __name__ == "__main__":
    test()

阅读(1640) | 评论(2) | 转发(0) |
0

上一篇:做报表~

下一篇:主机空间监控脚本

给主人留下些什么吧!~~

libin19832008-06-21 21:25:35

已补上. 注: select count(*) from v$session a where a.sid = '100'; 中 100 是挂掉的session的sid

libin19832008-06-21 18:38:41

真不好意思, 今天发现这篇文章不全. 我会尽快补上.