分类: Oracle
2010-11-30 18:29:40
#File: multithread.py
#Storing BLOBs in separate threads in parallel import cx_Oracle import threading from urllib import urlopen #subclass of threading.Thread class AsyncBlobInsert(threading.Thread): def __init__(self, cur, input): threading.Thread.__init__(self) self.cur = cur self.input = input def run(self): blobdoc = self.input.read() self.cur.execute("INSERT INTO blob_tab (ID, BLOBDOC) VALUES(blob_seq.NEXTVAL, :blobdoc)", {'blobdoc':blobdoc}) self.input.close() self.cur.close() #main thread starts here inputs = [] inputs.append(open('/tmp/figure1.bmp', 'rb')) inputs.append(urlopen('', 'rb')) dbconn = cx_Oracle.connect('usr', 'pswd', '127.0.0.1/XE',threaded=True) dbconn.autocommit = True for input in inputs: cur = dbconn.cursor() cur.setinputsizes(blobdoc=cx_Oracle.BLOB) print time.clock( ) - start th = AsyncBlobInsert(cur, input) th.start() |