python中并行执行调度的问题

OnTheRoad84 2016-12-12 11:22:11
配置文件1:记录数据库服务器的信息,如下:
[10.1.54.2]
HostIP = 10.1.54.2
DBType = Oracle
Rac = True
Port = 1521
Version = 12
Service_name = racdb
User= system
Password = oracle
QueryListFile = ora11g.conf
Tnsname =

[10.1.54.3]
HostIP = 10.1.54.3
DBType = Oracle
Rac = True
Port = 1521
Version = 12
Service_name = racdb
User= system
Password = s$^Sx123
QueryListFile = ora12c.conf
Tnsname =

配置文件2:记录需要执行的sql
[curr_date]
Sqlstr = select sysdate from dual
Period = 120
Priority = 1
NoDataFound = none

[maxprocs]
Sqlstr = select value "maxprocs" from v$parameter where name ='processes'
Period = 60

根据配置文件1的服务器列表,按照配置文件2中定义的sql及执行周期,去周期性查询数据库服务器的指标。我的想法是通过线程+sched模块来完成这个需求,但是目前的状况是,线程1创建后,就一直在周期性查询第一台服务器的指标。无法再创建其他线程?如下,是我的python代码(版本是3.5.2):python初学者,请大神指教。
# -*- coding:utf-8 -*-
# -*- author:chenyingzi -*-
# -*- descrpiton -*-
import configparser
import time, sched, os
import threading
import random
import cx_Oracle

class HostInfo(object):
'''
读取配置文件db.conf信息,根据传入的section,返回section中各参数的参数值
a=hostinfo('dbinfo1')
a.hostip则返回IP地址192.168.1.200
'''

def __init__(self, section,cf):
self.section = section

if cf.has_option(section, 'hostip'):
self.hostip = cf.get(section, 'hostip')
else:
self.hostip = ''

if cf.has_option(section, 'dbtype'):
self.dbtype = cf.get(section, 'dbtype')
else:
self.dbtype = 'Oracle'

if cf.has_option(section, 'port'):
self.port = int(cf.get(section, 'port'))
else:
self.port = 1521

if cf.has_option(section, 'version'):
self.version = cf.get(section, 'version')
else:
self.version = ''

if cf.has_option(section, 'rac'):
self.rac = cf.get(section, 'rac')
else:
self.rac = False

if cf.has_option(section, 'service_name'):
self.service_name = cf.get(section, 'service_name')
else:
self.service_name = ''

if cf.has_option(section, 'querylistfile'):
self.querylistfile = cf.get(section, 'querylistfile')
else:
self.querylistfile = ''

if cf.has_option(section, 'user'):
self.user = cf.get(section, 'user')
else:
self.user = ''

if cf.has_option(section, 'password'):
self.password = cf.get(section, 'password')
else:
self.password = ''

if cf.has_option(section, 'tnsname'):
self.tnsname = cf.get(section, 'tnsname')
else:
self.tnsname = ''

if cf.has_option(section, 'maxactive'):
self.maxactive = int(cf.get(section, 'maxactive'))
else:
self.maxactive = 5

if cf.has_option(section, 'maxwait'):
self.maxwait = int(cf.get(section, 'maxwait'))
else:
self.maxwait = 100

if cf.has_option(section, 'maxidle'):
self.maxidle = int(cf.get(section, 'maxidle'))
else:
self.maxidle = 1

def hostdic(self):

self.hostdic = {}
for option in cf.options(section):
self.hostdic[option] = cf[section][option]

return self.hostdic

def __str__(self):
return 'The DB detail as followed:' + \
'\nSection : ' + self.section +\
'\nHostIP : ' + self.hostip + \
'\nDBType : ' + self.dbtype + \
'\nRAC : ' + self.rac + \
'\nPort : ' + str(self.port) + \
'\nVersion : ' + self.version + \
'\nSrv_Name : ' + self.service_name + \
'\nUser : ' + self.user + \
'\nPassword : ' + self.password + \
'\nQueryFile: ' + self.querylistfile + \
'\nMaxActive: ' + str(self.maxactive) + \
'\nMaxWait : ' + str(self.maxwait) + \
'\nMaxIdle : ' + str(self.maxidle)

class QueryInfo(object):
def __init__(self, section,qf):
self.section = section
if qf.has_option(section, 'sqlstr'):
self.sqlstr = qf.get(section, 'sqlstr')
else:
print('No sql defined for section: '%section)

if qf.has_option(section, 'period'):
self.period = int(qf.get(section, 'period'))
else:
self.period = 300

if qf.has_option(section, 'priority'):
self.priority = int(qf.get(section, 'priority'))
else:
self.priority = random.randint(999,99999)

if qf.has_option(section, 'nodatafound'):
self.nodatafound = qf.get(section, 'nodatafound')
else:
self.nodatafound =''

def querydic(self):
self.dic = {}
for option in cf.options(section):
self.querydic[option] = qf[section][option].upper()
return self.querydic

def __str__(self):
return 'The Query detail as followed:' + \
'\nSqlstr : ' + self.sqlstr + \
'\nPeriod : ' + str(self.period) + \
'\nNoDataFound : ' + str(self.nodatafound)

def perform(hostip,sql,s):
print('IP: %s\nsqlname: %s\n sqlstr: %s\n period: %d \n priority: %d \ntime: %d\n' % (hostip, sql.section, sql.sqlstr, sql.period, sql.priority,time.time()))
s.enter(sql.period, sql.priority, perform, (hostip,sql,s,))

def hello3(hostip,querycfg):
print(threading.current_thread().name)
s = sched.scheduler(time.time, time.sleep)
for sqlsec in querycfg.sections():
sql = QueryInfo(sqlsec,querycfg)
s.enter(0, sql.period, perform, (hostip,sql,s,))
s.run()

#初始化ConfigParser类
hostcfg = configparser.ConfigParser()
#读取被控服务器列表配置文件
hostcfg.read('db.conf')

# #循环配置文件section
# 初始化ConfigParser类
querycfg = configparser.ConfigParser()
for section in hostcfg.sections():
host = HostInfo(section,hostcfg)

# 读取sql脚本文件
querycfg.read(host.querylistfile)
# print(querycfg)
#生成线程,处理sql语句
t1 = threading.Thread(target=hello3,args=(host.section,querycfg,),name='Thread for '+host.section)
t1.start()
t1.join()
...全文
375 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
屎克螂 2016-12-22
  • 打赏
  • 举报
回复
看着很乱,大概看了一下,没明白为啥sched与threading一起用 这两个不是相似的东西吗? def x(): whlie 1: print 111111 time.sleep(60) threading.Thread(target=x)
广印大叔 2016-12-20
  • 打赏
  • 举报
回复
t1 = threading.Thread(target=hello3,args=(host.section,querycfg,),name='Thread for '+host.section) t1.start() t1.join() 你这儿执行的时候 join 会一直等待到当前的thread执行结束的 join的注释你先看看 """Wait until the thread terminates. threads = list() for section in hostcfg.sections(): t1 = threading.Thread(target=hello3,args=(host.section,querycfg,),name='Thread for '+host.section) threads.appand(t1) t1.start() # t1.join() 不要在这儿执行join while True: alive = 0 for _thread in threads: if _thread.is_alive(): alive += 1 if not alive: break time.sleep(10)

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧