问题
I am running this code in my pyodbc script where I am trying to do parallelism
templst = [lineitem, orders, partsupp, region, cur_cur, T1, T2]
connstr = [DRIVER={libdbodbc17.so};host=lint16muthab.phl.sap.corp:8766;UID=dba;PWD=sql;CharSet=utf8, DRIVER={libdbodbc17.so};host=localhost:8767;UID=dba;PWD=sql;CharSet=utf8,
DRIVER={libdbodbc17.so};host=localhost:8768;UID=dba;PWD=sql;CharSet=utf8,
DRIVER={libdbodbc17.so};host=localhost:8769;UID=dba;PWD=sql;CharSet=utf8, DRIVER={libdbodbc17.so};host=localhost:8770;UID=dba;PWD=sql;CharSet=utf8]
def extract_single(q, cursorconn):
while True:
try:
tableName = q.get_nowait()
time.sleep(3)
qry2 = "Select * FROM %s"% (tableName)
print " extraction done of table:%s done by cursor:%s"%(tableName,cursorconn)
except Queue.Empty:
return
def main():
q = multiprocessing.Queue()
for item in templst:
q.put(item) # add items to queue
process = []
for i in xrange(5):
p = multiprocessing.Process(target=extract_single, args=(q, connstr[i]))
process.append(p)
p.start()
for p in process:
p.join()
if __name__ == '__main__':
main()
Output is like :
extraction done of table:lineitem done by cursor:DRIVER={libdbodbc17.so};host=lint16muthab.phl.sap.corp:8766;UID=dba;PWD=sql;CharSet=utf8
extraction done of table:orders done by cursor:DRIVER={libdbodbc17.so};host=localhost:8767;UID=dba;PWD=sql;CharSet=utf8
extraction done of table:partsupp done by cursor:DRIVER={libdbodbc17.so};host=localhost:8768;UID=dba;PWD=sql;CharSet=utf8
extraction done of table:region done by cursor:DRIVER={libdbodbc17.so};host=localhost:8769;UID=dba;PWD=sql;CharSet=utf8
extraction done of table:cur_cur done by cursor:DRIVER={libdbodbc17.so};host=localhost:8770;UID=dba;PWD=sql;CharSet=utf8
extraction done of table:T2 done by cursor:DRIVER={libdbodbc17.so};host=localhost:8767;UID=dba;PWD=sql;CharSet=utf8
extraction done of table:T1 done by cursor:DRIVER={libdbodbc17.so};host=lint16muthab.phl.sap.corp:8766;UID=dba;PWD=sql;CharSet=utf8
But when I modify my extract function into
def extract_single(q, cursorconn):
while True:
try:
tableName = q.get_nowait()
time.sleep(3)
conn = pyodbc.connect(cursorconn, timeout=0)
cursor = connvar.cursor()
qry2 = "Select * FROM %s"% (tableName)
cursor.execute(qry2).fetchall()
print " extraction done of table:%s done by cursor:%s"%(tableName,cursorconn)
except Queue.Empty:
return
I am getting error and not able to open a connection and cursor on that so that I can execute this query and dump data into files.
2Process Process-1:
Traceback (most recent call last):
File "/sybopt/software/python/python/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
self.run()
File "/sybopt/software/python/python/lib/python2.7/multiprocessing/process.py", line 114, in run
self._target(*self._args, **self._kwargs)
File "conn3.py", line 86, in extract_single
Process Process-2:
Traceback (most recent call last):
connvar = pyodbc.connect(cursorconn, timeout=0)
File "/sybopt/software/python/python/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
Error: ('IM005', "[IM005] [unixODBC][Driver Manager]Driver's SQLAllocHandle on SQL_HANDLE_DBC failed (0) (SQLDriverConnect)")
self.run()
File "/sybopt/software/python/python/lib/python2.7/multiprocessing/process.py", line 114, in run
self._target(*self._args, **self._kwargs)
File "conn3.py", line 86, in extract_single
connvar = pyodbc.connect(cursorconn, timeout=0)
Error: ('IM005', "[IM005] [unixODBC][Driver Manager]Driver's SQLAllocHandle on SQL_HANDLE_DBC failed (0) (SQLDriverConnect)")
Process Process-3:
Traceback (most recent call last):
File "/sybopt/software/python/python/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
self.run()
File "/sybopt/software/python/python/lib/python2.7/multiprocessing/process.py", line 114, in run
self._target(*self._args, **self._kwargs)
File "conn3.py", line 86, in extract_single
connvar = pyodbc.connect(cursorconn, timeout=0)
for all connections. Please let me know how to resolve this error. Error getting while using cursor object
I want to use cursor object which I have stored in a form of list instead of opening a new connection again and everytime open a cursor. cursorlist will look like
<pyodbc.Cursor object at 0x7fcd9b47f270> <pyodbc.Cursor object at 0x7fcd9b47f330> <pyodbc.Cursor object at 0x7fcd9b47f390> <pyodbc.Cursor object at 0x7fcd9b47f3f0> <pyodbc.Cursor object at 0x7fcd9b47f450>
So I have 5 cursor objects and table list same as above
def extract_single(q, cursorconn):
while True:
try:
tableName = q.get_nowait()
qry2 = "Select count(*) FROM %s"%(tableName)
cursorconn.execute(qry2)
rowcnt = cursorconn.fetchone()[0]
print " rows in tempdsc=",rowcnt
print " extraction done of table:%s done by cursor:%s"%(tableName,cursorconn)
except Queue.Empty:
return
def main():
q = multiprocessing.Queue()
for item in tempdsclst:
q.put(item) # add items to queue
process = []
for i in xrange(5):
p = multiprocessing.Process(target=extract_single, args=(q, curlst[i]))
process.append(p)
p.start()
for p in process:
p.join()
it gives error like
Process Process-1:
Traceback (most recent call last):
File "/sybopt/software/python/python/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
Process Process-2:
Traceback (most recent call last):
self.run()
File "/sybopt/software/python/python/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
File "/sybopt/software/python/python/lib/python2.7/multiprocessing/process.py", line 114, in run
self._target(*self._args, **self._kwargs)
File "conn4.py", line 87, in extract_single
cursorconn.execute(qry2)
Error: ('HY000', 'The driver did not supply an error!')
self.run()
File "/sybopt/software/python/python/lib/python2.7/multiprocessing/process.py", line 114, in run
self._target(*self._args, **self._kwargs)
File "conn4.py", line 87, in extract_single
cursorconn.execute(qry2)
Error: ('HY000', 'The driver did not supply an error!')
Process Process-3:
Traceback (most recent call last):
File "/sybopt/software/python/python/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
self.run()
File "/sybopt/software/python/python/lib/python2.7/multiprocessing/process.py", line 114, in run
self._target(*self._args, **self._kwargs)
File "conn4.py", line 87, in extract_single
cursorconn.execute(qry2)
Error: ('HY000', 'The driver did not supply an error!')
Process Process-4:
Traceback (most recent call last):
File "/sybopt/software/python/python/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
self.run()
File "/sybopt/software/python/python/lib/python2.7/multiprocessing/process.py", line 114, in run
self._target(*self._args, **self._kwargs)
File "conn4.py", line 87, in extract_single
cursorconn.execute(qry2)
Error: ('HY000', 'The driver did not supply an error!')
Process Process-5:
Traceback (most recent call last):
File "/sybopt/software/python/python/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
self.run()
File "/sybopt/software/python/python/lib/python2.7/multiprocessing/process.py", line 114, in run
self._target(*self._args, **self._kwargs)
File "conn4.py", line 87, in extract_single
cursorconn.execute(qry2)
Error: ('HY000', 'The driver did not supply an error!')
回答1:
Aside from the problem you are having, based on your earlier question I had assumed that cursorconn
was an already opened connection that would be reused for each query. Since it now appears to be just a connection string (although in your code it doesn't seem to be an actual string, which is the same situation for the items in list templst
???), then you should be making the call conn = pyodbc.connect(cursorconn, timeout=0)
before the while True:
statement so that the same connection can be reused for multiple queries and you should then close the connection before you return from the function.
I think the problem is the statement cursor = connvar.cursor()
, which should be: cursor = conn.cursor()
. So, try the following:
conn = pyodbc.connect(cursorconn, timeout=0)
while True:
try:
tableName = q.get_nowait()
time.sleep(3) # why is this here?
cursor = conn.cursor()
qry2 = "Select * FROM %s"% (tableName)
cursor.execute(qry2).fetchall()
print " extraction done of table:%s done by cursor:%s"%(tableName,cursorconn)
cursor.close() # should probably add this
except Queue.Empty:
return
finally:
conn.close()
来源:https://stackoverflow.com/questions/62237516/error-im005-im005-unixodbcdriver-managerdrivers-sqlallochandle-on-s