python 多线程 公用一个连接

#coding=utf-8
import threading
import time
import cx_Oracle
from pprint import pprint
import csv
table_name = "dbtest.csv"
f = open(table_name + ".csv", "w")
conn = cx_Oracle.connect('test/test@192.168.137.2/serv')
cursor = conn.cursor()
owner = "system"
def exp01():
    writer = csv.writer(f, lineterminator="
", quoting=csv.QUOTE_NONNUMERIC)
    tname = threading.current_thread()
    print tname
    exportOracleSql = "select 'exp01' from dual"
    print exportOracleSql
    x = cursor.execute(exportOracleSql)
    time.sleep(1000)
    cursor.close()
    conn.close()

def exp02():
    writer = csv.writer(f, lineterminator="
", quoting=csv.QUOTE_NONNUMERIC)
    tname = threading.current_thread()
    print tname
    exportOracleSql = "select 'exp02' from dual"
    print exportOracleSql
    x = cursor.execute(exportOracleSql)
    time.sleep(1000)
    cursor.close()
    conn.close()


def exp03():
        writer = csv.writer(f, lineterminator="
", quoting=csv.QUOTE_NONNUMERIC)
        tname = threading.current_thread()
        print tname
        exportOracleSql = "select 'exp03' from dual"
        print exportOracleSql
        x = cursor.execute(exportOracleSql)
        time.sleep(1000)
        cursor.close()
        conn.close()

def exp04():
        writer = csv.writer(f, lineterminator="
", quoting=csv.QUOTE_NONNUMERIC)
        tname = threading.current_thread()
        print tname
        exportOracleSql = "select 'exp04' from dual"
        print exportOracleSql
        x = cursor.execute(exportOracleSql)
        time.sleep(1000)
        cursor.close()
        conn.close()

def exp05():
        writer = csv.writer(f, lineterminator="
", quoting=csv.QUOTE_NONNUMERIC)
        tname = threading.current_thread()
        print tname
        exportOracleSql = "select 'exp05' from dual"
        print exportOracleSql
        x = cursor.execute(exportOracleSql)
        time.sleep(1000)
        cursor.close()
        conn.close()

def exp06():
        writer = csv.writer(f, lineterminator="
", quoting=csv.QUOTE_NONNUMERIC)
        tname = threading.current_thread()
        print tname
        exportOracleSql = "select 'exp06' from dual"
        print exportOracleSql
        x = cursor.execute(exportOracleSql)
        time.sleep(1000)
        cursor.close()
        conn.close()

def exp07():
        writer = csv.writer(f, lineterminator="
", quoting=csv.QUOTE_NONNUMERIC)
        tname = threading.current_thread()
        print tname
        exportOracleSql = "select 'exp07' from dual"
        print exportOracleSql
        x = cursor.execute(exportOracleSql)
        time.sleep(1000)
        cursor.close()
        conn.close()

def exp08():
        writer = csv.writer(f, lineterminator="
", quoting=csv.QUOTE_NONNUMERIC)
        tname = threading.current_thread()
        print tname
        exportOracleSql = "select 'exp08' from dual"
        print exportOracleSql
        x = cursor.execute(exportOracleSql)
        time.sleep(1000)
        cursor.close()
        conn.close()

def exp09():
        writer = csv.writer(f, lineterminator="
", quoting=csv.QUOTE_NONNUMERIC)
        tname = threading.current_thread()
        print tname
        exportOracleSql = "select 'exp09' from dual"
        print exportOracleSql
        x = cursor.execute(exportOracleSql)
        time.sleep(1000)
        cursor.close()
        conn.close()

def exp10():
        writer = csv.writer(f, lineterminator="
", quoting=csv.QUOTE_NONNUMERIC)
        tname = threading.current_thread()
        print tname
        exportOracleSql = "select 'exp10' from dual"
        print exportOracleSql
        x = cursor.execute(exportOracleSql)
        time.sleep(1000)
        cursor.close()
        conn.close()

def exp11():
        writer = csv.writer(f, lineterminator="
", quoting=csv.QUOTE_NONNUMERIC)
        tname = threading.current_thread()
        print tname
        exportOracleSql = "select 'exp11' from dual"
        print exportOracleSql
        x = cursor.execute(exportOracleSql)
        time.sleep(1000)
        cursor.close()
        conn.close()

def exp12():
        writer = csv.writer(f, lineterminator="
", quoting=csv.QUOTE_NONNUMERIC)
        tname = threading.current_thread()
        print tname
        exportOracleSql = "select 'exp12' from dual"
        print exportOracleSql
        x = cursor.execute(exportOracleSql)
        time.sleep(1000)
        cursor.close()
        conn.close()

def exp13():
        writer = csv.writer(f, lineterminator="
", quoting=csv.QUOTE_NONNUMERIC)
        tname = threading.current_thread()
        print tname
        exportOracleSql = "select 'exp13' from dual"
        print exportOracleSql
        x = cursor.execute(exportOracleSql)
        time.sleep(1000)
        cursor.close()
        conn.close()

def exp14():
        writer = csv.writer(f, lineterminator="
", quoting=csv.QUOTE_NONNUMERIC)
        tname = threading.current_thread()
        print tname
        exportOracleSql = "select 'exp14' from dual"
        print exportOracleSql
        x = cursor.execute(exportOracleSql)
        time.sleep(1000)
        cursor.close()
        conn.close()

threads = []
t1 = threading.Thread(target=exp01,name='exp01')
threads.append(t1)
t2 = threading.Thread(target=exp02,name='exp02')
threads.append(t2)
t2 = threading.Thread(target=exp03,name='exp03')
threads.append(t2)
t2 = threading.Thread(target=exp04,name='exp04')
threads.append(t2)
t2 = threading.Thread(target=exp05,name='exp05')
threads.append(t2)
t2 = threading.Thread(target=exp06,name='exp06')
threads.append(t2)
t2 = threading.Thread(target=exp07,name='exp07')
threads.append(t2)
t2 = threading.Thread(target=exp08,name='exp08')
threads.append(t2)
t2 = threading.Thread(target=exp09,name='exp09')
threads.append(t2)
t2 = threading.Thread(target=exp10,name='exp10')
threads.append(t2)
t2 = threading.Thread(target=exp11,name='exp11')
threads.append(t2)
t2 = threading.Thread(target=exp12,name='exp12')
threads.append(t2)
t2 = threading.Thread(target=exp13,name='exp13')
threads.append(t2)
t2 = threading.Thread(target=exp14,name='exp14')
threads.append(t2)

if __name__ == '__main__':
    for t in threads:
        #t.setDaemon(True)
        t.start()
        #t.run()
        #t.start()
        # print '3333333'
        print threading.current_thread()
        # print t.is_alive()
        # print '3333333'
    t.join()
    print "all over "

[oracle@node01 python]$ python t100.py 
<Thread(exp01, started 140505364154112)><_MainThread(MainThread, started 140505545430784)>

select 'exp01' from dual
<Thread(exp02, started 140505353656064)>
select 'exp02' from dual
<_MainThread(MainThread, started 140505545430784)>
<_MainThread(MainThread, started 140505545430784)>
<Thread(exp03, started 140505343166208)>
select 'exp03' from dual
<Thread(exp04, started 140505332676352)><_MainThread(MainThread, started 140505545430784)>

select 'exp04' from dual
Exception in thread exp01:
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/threading.py", line 551, in __bootstrap_inner
    self.run()
  File "/usr/local/lib/python2.7/threading.py", line 504, in run
    self.__target(*self.__args, **self.__kwargs)
  File "t100.py", line 18, in exp01
    x = cursor.execute(exportOracleSql)
DatabaseError: DPI-1002: invalid OCI handle

<Thread(exp05, started 140505322186496)>
select 'exp05' from dual
 <_MainThread(MainThread, started 140505545430784)>
<_MainThread(MainThread, started 140505545430784)>
<Thread(exp06, started 140505364154112)>
select 'exp06' from dual
<_MainThread(MainThread, started 140505545430784)>
<Thread(exp07, started 140505311696640)>
select 'exp07' from dual
Exception in thread exp04:
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/threading.py", line 551, in __bootstrap_inner
    self.run()
  File "/usr/local/lib/python2.7/threading.py", line 504, in run
    self.__target(*self.__args, **self.__kwargs)
  File "t100.py", line 52, in exp04
    x = cursor.execute(exportOracleSql)
DatabaseError: DPI-1002: invalid dpiStmt handle

<Thread(exp08, started 140504956139264)>
select 'exp08' from dual
<_MainThread(MainThread, started 140505545430784)>
<Thread(exp09, started 140505332676352)>
Exception in thread exp03:
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/threading.py", line 551, in __bootstrap_inner
    self.run()
  File "/usr/local/lib/python2.7/threading.py", line 504, in run
    self.__target(*self.__args, **self.__kwargs)
  File "t100.py", line 41, in exp03
    x = cursor.execute(exportOracleSql)
OperationalError: ORA-01013: 用户请求取消当前的操作

select 'exp09' from dual
<_MainThread(MainThread, started 140505545430784)>
<_MainThread(MainThread, started 140505545430784)>
Exception in thread exp06:
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/threading.py", line 551, in __bootstrap_inner
    self.run()
  File "/usr/local/lib/python2.7/threading.py", line 504, in run
    self.__target(*self.__args, **self.__kwargs)
  File "t100.py", line 74, in exp06
    x = cursor.execute(exportOracleSql)
DatabaseError: ORA-24315: 非法的属性类型

<Thread(exp10, started 140505343166208)>
select 'exp10' from dual
<Thread(exp11, started 140504945649408)>
select 'exp11' from dual
<_MainThread(MainThread, started 140505545430784)>
<_MainThread(MainThread, started 140505545430784)>
<Thread(exp12, started 140505364154112)>
select 'exp12' from dual
Exception in thread exp09:
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/threading.py", line 551, in __bootstrap_inner
    self.run()
  File "/usr/local/lib/python2.7/threading.py", line 504, in run
    self.__target(*self.__args, **self.__kwargs)
  File "t100.py", line 107, in exp09
    x = cursor.execute(exportOracleSql)
OperationalError: ORA-01013: 用户请求取消当前的操作

<Thread(exp13, started 140504935159552)>
select 'exp13' from dual
Exception in thread exp10:
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/threading.py", line 551, in __bootstrap_inner
    self.run()
  File "/usr/local/lib/python2.7/threading.py", line 504, in run
    self.__target(*self.__args, **self.__kwargs)
  File "t100.py", line 118, in exp10
    x = cursor.execute(exportOracleSql)
OperationalError: ORA-03114: 未连接到 ORACLE

Exception in thread exp11:
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/threading.py", line 551, in __bootstrap_inner
    self.run()
  File "/usr/local/lib/python2.7/threading.py", line 504, in run
    self.__target(*self.__args, **self.__kwargs)
  File "t100.py", line 129, in exp11
    x = cursor.execute(exportOracleSql)
OperationalError: ORA-03114: 未连接到 ORACLE

Exception in thread exp12:
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/threading.py", line 551, in __bootstrap_inner
    self.run()
  File "/usr/local/lib/python2.7/threading.py", line 504, in run
    self.__target(*self.__args, **self.__kwargs)
  File "t100.py", line 140, in exp12
    x = cursor.execute(exportOracleSql)
DatabaseError: DPI-1002: invalid dpiStmt handle

Exception in thread exp07:
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/threading.py", line 551, in __bootstrap_inner
    self.run()
  File "/usr/local/lib/python2.7/threading.py", line 504, in run
    self.__target(*self.__args, **self.__kwargs)
  File "t100.py", line 85, in exp07
    x = cursor.execute(exportOracleSql)
DatabaseError: ORA-24909: 正在执行调用。当前的操作已取消

<_MainThread(MainThread, started 140505545430784)>
<_MainThread(MainThread, started 140505545430784)>
<Thread(exp14, started 140504945649408)>
select 'exp14' from dual
Exception in thread exp14:
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/threading.py", line 551, in __bootstrap_inner
    self.run()
  File "/usr/local/lib/python2.7/threading.py", line 504, in run
    self.__target(*self.__args, **self.__kwargs)
  File "t100.py", line 162, in exp14
    x = cursor.execute(exportOracleSql)
OperationalError: ORA-03114: 未连接到 ORACLE

all over 
Exception in thread exp13:
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/threading.py", line 551, in __bootstrap_inner
    self.run()
  File "/usr/local/lib/python2.7/threading.py", line 504, in run
    self.__target(*self.__args, **self.__kwargs)
  File "t100.py", line 151, in exp13
    x = cursor.execute(exportOracleSql)
OperationalError: ORA-03114: 未连接到 ORACLE

Errors in file :
OCI-21500: 内部错误代码, 参数: [17099], [], [], [], [], [], [], []
OCI-21500: 内部错误代码, 参数: [17099], [], [], [], [], [], [], []
Errors in file :
OCI-21500: 内部错误代码, 参数: [17099], [], [], [], [], [], [], []
OCI-21500: 内部错误代码, 参数: [17099], [], [], [], [], [], [], []
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ.їїїїїїїїSegmentation fault (core dumped)



DatabaseError: ORA-24909: 正在执行调用。当前的操作已取消


[oracle@node01 python]$ oerr ora 24909
24909, 00000, "call in progress. Current operation cancelled"
// *Cause: The OCI call was invoked when another call on the connection was
//         in progress.
// *Action: Check if the OCI call is supported when the call is in progress
//          under special conditions; for example, if it is being used by a
//          signal handler.
// NLS_DO_NOT_TRANSLATE [24910,24910]
[oracle@node01 python]$ 

当多个线程调用一个数据库连接的时候

OCI 被调用当另外一个请求是正在处理

并发连接出现的错误
应该是单个连接同时对数据库进行了操作,尝试并发连接中!

原文地址:https://www.cnblogs.com/hzcya1995/p/13349543.html