Python cx_oracle 多线程不适用于每个线程的游标

Sim*_*lls 3 python oracle multithreading cx-oracle

我正在尝试在 python 中使用 cx_Oracle 并行运行完全独立的 Oracle 查询。

我可以通过为每个线程设置一个新的数据库连接,然后在每个单独的线程中运行查询来成功地完成这项工作,这使总时间从大约 2 分钟到 1 分钟 20 分,所以它肯定可以工作。查询时间:

START_TIME                      END_TIME
17-FEB-16 22.33.28.000000000    17-FEB-16 22.33.30.000000000
17-FEB-16 22.33.30.000000000    17-FEB-16 22.33.33.000000000
17-FEB-16 22.33.33.000000000    17-FEB-16 22.33.36.000000000
17-FEB-16 22.33.36.000000000    17-FEB-16 22.33.36.000000000
17-FEB-16 22.33.36.000000000    17-FEB-16 22.34.08.000000000
17-FEB-16 22.34.08.000000000    17-FEB-16 22.34.26.000000000
17-FEB-16 22.34.26.000000000    17-FEB-16 22.34.27.000000000
17-FEB-16 22.34.27.000000000    17-FEB-16 22.34.29.000000000
Run Code Online (Sandbox Code Playgroud)

但是,在每个线程中设置与数据库的连接会产生开销,我很确定我应该能够为每个线程创建一个新游标并共享连接,如下所示:

http://www.oracle.com/technetwork/articles/vasiliev-python-concurrency-087536.html

但是,当我共享连接并使用单独的游标时会发生什么情况是查询全部同时开始然后同时结束,因此看起来在生成线程时,在数据库上查询仍在按顺序运行. 查询时间:

START_TIME                      END_TIME
17-FEB-16 22.36.32.000000000    17-FEB-16 22.38.21.000000000
17-FEB-16 22.36.32.000000000    17-FEB-16 22.38.21.000000000
17-FEB-16 22.36.32.000000000    17-FEB-16 22.38.21.000000000
17-FEB-16 22.36.31.000000000    17-FEB-16 22.38.21.000000000
17-FEB-16 22.36.31.000000000    17-FEB-16 22.38.21.000000000
17-FEB-16 22.36.31.000000000    17-FEB-16 22.38.21.000000000
17-FEB-16 22.36.31.000000000    17-FEB-16 22.38.21.000000000
Run Code Online (Sandbox Code Playgroud)

多连接代码:

for file in file_transporter.complete_file_list:
        #Get database and open connection
        the_db =      shared_lib_wrapper.get_oracle().Oracle(the_logger)
        the_db .connect(conn_str())
        #Create new thread
        thread = threading.Thread(target=Loader, args=(params, the_date, the_logger, the_db, file, file_transporter.complete_file_list[file]))
        the_logger.info("Running Thread: " + thread.getName())
        thread.start()
Run Code Online (Sandbox Code Playgroud)

多游标代码(在 runLoad 中有一个创建新游标并执行过程的函数 - 见下文):

for file in self.file_list:
        file_parametes = self.file_list[file]
        function_to_run = file_parametes['LOAD_PACKAGE'] + '.' + file_parametes['LOAD_FUNCTION']

        #Create new thread
        thread = threading.Thread(target=self.runLoad, args=(file_parametes['RUN_ID'], function_to_run))
        self.log.info("Spawned Thread: " + thread.getName())
        self.log.info("Running Thread: " + thread.getName())
        thread.start()
Run Code Online (Sandbox Code Playgroud)

创建游标的代码:

def execute_stored_proc_with_in_and_out_params(self, proc_name, params, dbms_logging=False):
    try:
        cursor = cx_Oracle.Cursor(self.db_conn
Run Code Online (Sandbox Code Playgroud)

因此,我的问题是:

1)我在创建游标时做错了什么吗?- 如果有关于如何修复它的任何想法,我已经读过 cx_oracle 是线程安全 2:

Currently 2, which means that threads may share the module and connections, but not cursors.
Run Code Online (Sandbox Code Playgroud)

2)如果我不能共享连接,为每个线程创建一个新的连接有什么问题,即使创建每个连接的开销似乎仍然可以提高我的性能?

Ant*_*nga 5

请参阅下面的程序的工作实现,该程序使用相同的连接但每个线程中有一个单独的游标。我正在调用的过程在 cx_Oracle 测试用例(5.2.1 版本的一部分)中并且非常简单,所以我在示例中多次调用它(每个线程中的随机数)。输出清楚地表明线程不会同时完成。

from __future__ import print_function

import cx_Oracle
import datetime
import random
import threading

connection = cx_Oracle.Connection("cx_Oracle/dev", threaded = True)

def TestThread(threadNum):
     startTime = datetime.datetime.today()
     cursor = connection.cursor()
     numInputs = int(random.random() * 5000)
     print("Thread", threadNum, "with", numInputs, "inputs:", startTime)
     for i in range(numInputs):
         value = bool(int(random.random() * 2))
         cursor.callfunc("pkg_TestBooleans.GetStringRep", str, (value,))
     endTime = datetime.datetime.today()
     print("Thread", threadNum, "with", numInputs, "inputs:", endTime)

 threads = []
 for i in range(8):
     thread = threading.Thread(target = TestThread, args = (i + 1,))
     threads.append(thread)
     thread.start()
 print("All threads spawned...waiting for them to complete...")
 for thread in threads:
     thread.join()
Run Code Online (Sandbox Code Playgroud)

输出如下:

线程1与3405个输入:2016年2月22日07:55:07.849127
线程2与2706输入:2016年2月22日07:55:07.849998
线程3与4101输入:2016年2月22日07:55:07.850256
螺纹4与2912个输入:2016年2月22日07:55:07.850937
螺纹5与3747个输入:2016年2月22日07:55:07.851275
螺纹6与4318个输入:2016年2月22日07:55:07.851534
螺纹7 1453输入:2016-02-22 07:55:07.852649
带有 3304 个输入的线程 8:2016-02-22 07:55:07.853090
所有线程都产生了......等待它们完成......
带有 1453 个输入的线程 7:2016 02-22 07:55:09.897217
具有 2706 个输入的线程 2:2016-02-22 07:55:11.446744
具有 2912 个输入的线程 4:2016-02-22 07:55:114.681
螺纹8与3304个输入:2016年2月22日07:55:12.016809
线程1与3405输入:2016年2月22日07:55:12.081846
螺纹5与3747输入:2016年2月22日07:55:12.266111
线程3具有 4101 个输入:2016-02-22 07:55:12.375623
具有 4318 个输入的线程 6:2016-02-22 07:55:12.409352