标签:
最近由于项目开发中发现python informixDB模块对多线程的支持非常不好,当开启两个线程同时连接informix数据库的时候,数据库会报错,显示SQL process正在进行当中,根据python 多线程的机制我们怀疑是连接数据库时,informix将两个线程的cursor识别为同一个,故数据库报错。通过python中multiprocess模块讲所有关于数据库的操作全部改为多进程。
但是这就带来另外一个问题,在之前多线程的情况下,项目中维护着一个Queue,里面存储着若干已经创建好的informix tenant pool 的instance信息,用于客户快速获取可用数据库资源。但是有了多进程的存在,每次客户取一个instance,主进程都需要从Queue中取take一个出来,与此同时take()操作会触发创建一个tenant instance,里边包含一个进程将instance信息再次存储到Queue中。这里会涉及到Queue的进程间通讯的问题。需要将Queue改为multiprocess.queue才能避免数据丢失。这里我想尝试一下用内存数据库来试着简化进程通信的步骤。
Base
from module PyDbLite : from PyDbLite import BasecPickle
module : strings, Unicode strings, integers, floats, dates and datetimes (instances of the date
and datetime
classes in the datetime
module), user-defined classes, etccreate()
method, to specify what you want to do if the base already exists in the file system
IOError
is raisedNone
create()
methodcreate()
method, an internal field called __id__
is added. It is a integer which is guaranteed to be unique and unchanged for each record in the base, so that it can be used as the record identifier__version__
is also managed by the database engine. It is a integer which is set to 0 when the record is created, then incremented by 1 each time the record is updated. This is used to detect concurrency control, for instance in a web application where 2 users select the same record and want to update it at the same time_age
: note the heading underscore, to avoid name conflicts with internal names). This attribute is a dictionary-like object, where keys are the values taken by the field, and values are the records whose field values are egal to the key : keys()
method returns all existing values for the fieldlist_of_records
can be any iterable (list, tuple, set, etc) yielding recordsNone
import pydblite # 使用内存数据库 pydb = pydblite.Base("address") # 创建a,b,c三个字段 pydb.create(‘a‘, ‘b‘, ‘c‘) # 为字段a,b创建索引 pydb.create_index(‘a‘, ‘b‘) # 插入一条数据 pydb.insert(a=0, b=0, c=1) pydb.insert(a=1, b=0, c=1) pydb.insert(a=1, b=0, c=1) pydb.insert(a=1, b=0, c=1) pydb.update(records=pydb[1],a=2,c="li") pydb.delete(pydb[0]) # 查询符合特定要求的数据 results = pydb(a=2) for i in results: print results[i]
标签:
原文地址:http://www.cnblogs.com/lirunzhou/p/5867044.html