给程序添加了数据库组件 - Jacky Liu's Blog

给程序添加了数据库组件

Jacky Liu posted @ 2010年10月11日 10:37 in Python with tags mysql python subprocess thread queue Queue Popen threading , 4043 阅读

给程序添加了数据库组件,跑通了第一个测试任务。

数据库组件的内容:

1. 数据库组件包含一个数据库接口,用 subprocess.Popen() 对象实现,负责连上外部的 MySQL 服务器进程。具体见前面

2. 包含一个任务队列,用 queue.Queue() 对象实现,内含 Query 任务对象,保证不同客户线程提交的 Query 任务被顺序执行。

3. 包含一个主控线程,用 threading.Thread() 对象实现,负责管理 Query 任务队列,逐个提取任务并执行。

4. 包含一个底层界面函数,负责向任务队列中添加任务对象。

5. 包含数目可扩展的多个高层界面函数,内部调用底层界面函数完成任务对象的添加,外部客户线程通过调用这些函数来完成数据库维护和查询任务。

6. 包含 Logger 对象,用 logging.Logger 实现,用来记录日志。

数据库组件作为主程序的一部分,在程序启动时初始化,向程序其它部分提供数据库查询服务。程序退出时,主程序通过调用合适的界面函数向任务队列里加入一个 “毒药”,主控线程提取到这个 “毒药” 后,就会拒绝接受新的查询任务,并启动组件的退出过程。

所有的 Query 任务对象,不论执行哪种查询任务,都必须符合一定的接口规范,所以使用类的继承机制是个自然选择。以下是 Query 对象基类的设计:

# -*- coding: utf-8 -*-

import threading

class QueryBase:
	
	def __init__(self, ilogger, kargs):
		self._ilogger= ilogger		# 提交任务的客户线程携带的 Logger 对象。
		self._name= kargs['name']	# kargs 是客户线程提交的信息,dict 类型,不同派生类有不同的内容
		
		self._querystr= self._generate_querystr()	# 发往 MySQL Server 的输入
		self._outstr= ''		# MySQL Server 的输出

		# Parser 线程对象,先建立起来,暂时不运行。
		self._parser= threading.Thread(target= self._thread_parser, name= self._name + '_parser')

		self._result= None	# 根据 self._outstr 处理得到的 Python 数据结构,由 self._thread_parser() 负责填充

		self._querydone= threading.Event()	# 通知提交 Query 任务的客户线程任务已完成。由 self._thread_parser() 负责置位。



	def _generate_querystr(self):
		'''
		负责根据初始化参数 kargs 里的值生成要送往 MySQL Server 的输入语句。
		'''
		return ''


	def _thread_parser(self):
		'''
		负责对 self._outstr 进行处理,得到 self._result 数据结构,并最终置位 self._querydone。
		'''
		pass


	def wait(self):
		'''
		Database 模块的界面函数调用此函数来阻塞主调的客户线程,直到查询任务完成。
		'''
		self._querydone.wait()


	def start(self, dbif):
		'''
		此函数在 Database 模块的主控线程内运行,主控线程通过调用此函数来执行查询任务。
		
		参数:
			dbif: 由 Database 模块的主控线程提供的一个接口函数,本函数通过调用此函数来获得 MySQL 服务器的输出。

		执行过程:
			1. 利用 dbif 获得 MySQL 服务器的输出,放在 self._outstr 里面。
			2. 开始执行 self._parser 线程对象。self._parser 将负责填充 self._result 数据结构,并置位 self._querydone
			3. self._parser 开始执行后此函数就可以退出,将控制权交还 Database 模块主控线程。
		'''

		self._outstr= dbif(ilogger=self._ilogger, querystr=self._querystr)
		self._parser.start()		# 开始执行 self._parser 线程对象。



Avatar_small
happy wheels 说:
2018年10月25日 10:07

包含数目可扩展的多个高层界面函数,内部调用底层界面函数完成任务对象的添加,外部客户线程通过调用这些函数来完成数据库维护和查询任务

Avatar_small
Emma 说:
2023年1月31日 20:44

Great work! This update to the program is sure to make it even more efficient by adding database components. This will allow Lab grown diamonds users to store and access data more quickly and easily, making their experience even better.

Avatar_small
anonymous 说:
2023年8月31日 12:52

Your blogs further more each else volume is so entertaining further serviceable It appoints me befall retreat encore. I will instantly grab your rss feed to stay informed of any updates. Pest Control Kissimmee

Avatar_small
anonymous 说:
2023年9月05日 19:51

You ought to basically fantastic not to mention solid advice, which means notice: Pest Control Orlando

Avatar_small
anonymous 说:
2023年9月05日 19:54

I also wrote an article on a similar subject will find it at       write what you think. Pest Control Oviedo

Avatar_small
anonymous 说:
2023年10月15日 21:07

For this web site, you will see our account, remember to go through this info. CPA Tax preparer

Avatar_small
anonymous 说:
2023年10月25日 02:13

These things are very important, good think so - I think so too...  ball bearings online

Avatar_small
anonymous 说:
2023年10月25日 22:01

מוניות לשדה התעופה בן גוריוןבלחיצת כפתור, שירות מוניות אדיב ומהיר לנתב"ג ובחזרה מתל אביב ישירות ליעד. הזמינו מונית לשדה התעופה.

 

Avatar_small
anonymous 说:
2023年10月26日 15:19

The most interesting text on this interesting topic that can be found on the net ...  assistance apple particuliers Geneve

Avatar_small
anonymous 说:
2023年10月31日 18:15

Cool you write, the information is very good and interesting, I'll give you a link to my site.  meuf

Avatar_small
anonymous 说:
2023年10月31日 19:00

It is especially decent, though look into the tips during this home address. bookkeeping food and beverage

Avatar_small
anonymous 说:
2023年11月03日 20:33

This is exciting, nevertheless it is vital for you to visit this specific url: where to sell your car in singapore

Avatar_small
anonymous 说:
2023年11月05日 19:25

Welcome to the party of my life      here you will learn everything about me. Artículo completo

Avatar_small
anonymous 说:
2023年11月08日 18:56

This content is simply exciting and creative. I have been deciding on a institutional move and this has helped me with one aspect. happie

Avatar_small
anonymous 说:
2023年11月09日 18:38

On this page, you'll see my profile, please read this information.  gbl kaufen legal

Avatar_small
anonymous 说:
2023年11月09日 18:46

Cool you write, the information is very good and interesting, I'll give you a link to my site.  achat herbe

Avatar_small
anonymous 说:
2023年11月12日 21:03

I read this article. I think You put a lot of effort to create this article. I appreciate your work. history of graph paper

Avatar_small
anonymous 说:
2023年11月16日 20:06

Welcome to the party of my life      here you will learn everything about me. comprar carta de condução verdadeira

Avatar_small
anonymous 说:
2023年11月19日 06:20

I should say only that its awesome! The blog is informational and always produce amazing things. 마바리판카지노사이트추천

Avatar_small
anonymous 说:
2023年12月11日 16:30

I really appreciate this wonderful post that you have provided for us. I assure this would be beneficial for most of the people. startup attorney

Avatar_small
anonymous 说:
2024年1月01日 00:29

Interesting and interesting information can be found on this topic here      profile worth to see it. 토토사이트 추천

Avatar_small
anonymous 说:
2024年1月08日 10:11

Interestingly you write, I will address        you'll find exciting and interesting things on similar topics. betso88

Avatar_small
anonymous 说:
2024年1月18日 14:30

On my website you'll see similar texts, write what you think.  slot69 login


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter
Host by is-Programmer.com | Power by Chito 1.3.3 beta | © 2007 LinuxGem | Design by Matthew "Agent Spork" McGee