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

标签 python multithreading oracle 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

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

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

多连接代码:

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()

多游标代码(在 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()

创建游标的代码:

def execute_stored_proc_with_in_and_out_params(self, proc_name, params, dbms_logging=False):
    try:
        cursor = cx_Oracle.Cursor(self.db_conn

因此我的问题是:

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

Currently 2, which means that threads may share the module and connections, but not cursors.

最佳答案

请看下面,这是一个程序的有效实现,它使用相同的连接,但每个线程中都有一个单独的游标。我调用的过程在 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()

输出如下:

具有 3405 个输入的线程 1:2016-02-22 07:55:07.849127
具有 2706 个输入的线程 2:2016-02-22 07:55:07.849998
具有 4101 个输入的线程 3:2016-02-22 07:55:07.850256
具有 2912 个输入的线程 4:2016-02-22 07:55:07.850937
具有 3747 个输入的线程 5:2016-02-22 07:55:07.851275
具有 4318 个输入的线程 6:2016-02-22 07:55:07.851534
具有 1453 个输入的线程 7: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:11.681414
具有 3304 个输入的线程 8:2016-02-22 07:55:12.016809
具有 3405 个输入的线程 1:2016-02-22 07:55:12.081846
具有 3747 个输入的线程 5:2016-02-22 07:55:12.266111
具有 4101 个输入的线程 3:2016-02-22 07:55:12.375623
具有 4318 个输入的线程 6:2016-02-22 07:55:12.409352

更新:如评论中所述,连接一次仅执行一项事件。因此,尽管在多个线程中使用来自同一连接的多个游标是线程安全,但它实际上并没有提高并发性。为此,您必须使用单独的连接。

关于Python cx_oracle 多线程不适用于每个线程的游标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35469431/

相关文章:

sql - 如何从系统日期中减去年份

java - 无法通过java连接oracle 11g

java - 在 Java 中使用 Thread.yield()

python - 合并列表python中的所有元素

python - 是否可以用另一种语言为一种语言编写自动垃圾收集器?

python - 属性错误: 'function' object has no attribute 'preprocess_input'

java - 在锁定对象上调用 wait() 之前调用 notify() 可以吗?

Java 双重检查锁定

sql - Oracle SQL 中多重集映射的意外结果

python - python中的部分阴影直方图