多线程 调用oracle函数,Python cx_oracle多线程处理不适用于每个线程的游标

我尝试使用cx_Oracle在python中并行运行完全独立的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

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

但是,当我共享连接并使用一个单独的游标时,会发生的情况是,所有查询同时开始,然后在同一时间结束,这样看来,当线程被派生时,数据库上的查询仍在按顺序运行。查询计时:

^{pr2}$

多连接代码: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峎u oracle是threadsafety 2:Currently 2, which means that threads may share the module and connections, but not cursors.

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值