From: R D. <rd...@cr...> - 2002-01-17 00:44:36
|
I am getting the following namerror when I try to run the code below: Traceback (innermost last): (no code object) at line 0 TypeError: run() takes at least 6 arguments (1 given) Traceback (innermost last): File "mtcxjdbcTester.py", line 76, in run AttributeError: 'int' object has no attribute 'incr' Total time to run 1 queries: 319.9999 ms Thanks for any insight you might have, Raj Here is the jython code: import sys,os,threading from java.lang import Thread from time import sleep, time from java.sql import DriverManager from time import time #from org.gjt.mm.mysql import Driver from com.cxa.jdbc import Driver from java.lang.Class import forName class keepCount: def __init__(self): self.reccount = 0 def incr(self,delta): self.reccount = self.reccount + delta def getreccount(self): return self.reccount class doStats(Thread): def __init__(self, id, pollint,k,c,iter): self.id = id self.c = c print c self.iter = iter self.pollint = pollint self.k = k print '%d'%(k.getreccount()) self.tstart = time() print "Starting doStats, thread #%d"%(self.id) self.start() def run(self): self.id = id self.c = c print c self.iter = iter self.pollint = pollint self.k = k rCount = 0 while (rCount < self.iter*self.c): sleep(pollint) lock.acquire() rCount=k.getreccount() lock.release() print '%d records fetched in %d ms'%(rCount,self.tstart-time()) class getRows(Thread): def __init__(self, id, con,iter,k): self.id = id self.iter = iter self.con = con self.k = k # print "Starting thread #%d"%(self.id) self.start() def run(self): self.iter = iter self.file = open("thread%d.out"%(self.id), "w") self.start = time() self.k = k pstmt = self.con.prepareStatement("select prodid,desc,cost from bea.prodroot where prodid = ?") print >>self.file,"###############" print >>self.file,"Time to prepare: %4.4f"%(1000*(time() - self.start)) for loopc in range(iter): self.start = time() keyv = 10000+self.id pstmt.setString(1,str(keyv)) rs = pstmt.executeQuery() print >>self.file,"Time to execute: %4.4f"%(1000*(time() - self.start)) self.start = time() nrec = 0 while rs.next() : print >>self.file,"Loop %d %s %s %s"%(loopc,rs.getString(1),rs.getString(2),rs.getString(3)) nrec = nrec + 1 rs.close() print >>self.file,"Time to get %d records: %4.4f"%(nrec, 1000*(time() - self.start)) lock.acquire() k.incr(nrec) lock.release() self.file.close() server, db,usr, passwd = "RAJDATTA", "test","rdatta", "rdatta" driver = "com.cxa.jdbc.Driver" #driver = "org.gjt.mm.mysql.Driver" forName(driver).newInstance() #url = "jdbc:mysql://%s/%s?user=%s&password=%s" % (server, db, usr, passwd) url = "jdbc:cxa:CXARAJ:tcp/P39D/8098" threadList = [] conList= [] connCount = int(sys.argv[1]) iter = int(sys.argv[2]) if (len(sys.argv) > 3): pollint = int(sys.argv[3]) else: pollint = 30 start = time() for i in xrange(0,connCount): conList.insert(i,DriverManager.getConnection(url)) print 'Total time to create %d connections: %.4f ms' % (connCount,1000*(time()-start)) #del threadList #threadList = [] lock = threading.Lock() start = time() k=0 kObj = keepCount(); print 'reccount %d'%(kObj.getreccount()) for j in range(connCount): k=k+1 threadList.append(getRows(k,conList[j],iter,kObj)) sleep(0.1) print "Total threads running: ",k threadList.append(doStats(k+1,pollint,kObj,connCount,iter)) for tTh in threadList: tTh.join() elapsed = time() - start print 'Total time to run %d queries: %.4f ms' % (k*iter,1000*elapsed) Thanks Raj Datta Professional Services Direct : 408-530-4932 CrossAccess Corp Cell : 408-316-5473 2900 Gordon Ave #100 Fax : 408-735-0328 Santa Clara CA 95051 Email : rd...@cr... |
From: R D. <rd...@cr...> - 2002-01-17 00:46:00
|
Sorry, I sent the wrong error message. Here is the correct one: I am getting the following namerror when I try to run the code below: Traceback (innermost last): File "mtcxjdbcTester.py", line 31, in run NameError: c Traceback (innermost last): File "mtcxjdbcTester.py", line 76, in run AttributeError: 'int' object has no attribute 'incr' Thanks for any insight you might have, Raj Here is the jython code: import sys,os,threading from java.lang import Thread from time import sleep, time from java.sql import DriverManager from time import time #from org.gjt.mm.mysql import Driver from com.cxa.jdbc import Driver from java.lang.Class import forName class keepCount: def __init__(self): self.reccount = 0 def incr(self,delta): self.reccount = self.reccount + delta def getreccount(self): return self.reccount class doStats(Thread): def __init__(self, id, pollint,k,c,iter): self.id = id self.c = c print c self.iter = iter self.pollint = pollint self.k = k print '%d'%(k.getreccount()) self.tstart = time() print "Starting doStats, thread #%d"%(self.id) self.start() def run(self): self.id = id self.c = c print c self.iter = iter self.pollint = pollint self.k = k rCount = 0 while (rCount < self.iter*self.c): sleep(pollint) lock.acquire() rCount=k.getreccount() lock.release() print '%d records fetched in %d ms'%(rCount,self.tstart-time()) class getRows(Thread): def __init__(self, id, con,iter,k): self.id = id self.iter = iter self.con = con self.k = k # print "Starting thread #%d"%(self.id) self.start() def run(self): self.iter = iter self.file = open("thread%d.out"%(self.id), "w") self.start = time() self.k = k pstmt = self.con.prepareStatement("select prodid,desc,cost from bea.prodroot where prodid = ?") print >>self.file,"###############" print >>self.file,"Time to prepare: %4.4f"%(1000*(time() - self.start)) for loopc in range(iter): self.start = time() keyv = 10000+self.id pstmt.setString(1,str(keyv)) rs = pstmt.executeQuery() print >>self.file,"Time to execute: %4.4f"%(1000*(time() - self.start)) self.start = time() nrec = 0 while rs.next() : print >>self.file,"Loop %d %s %s %s"%(loopc,rs.getString(1),rs.getString(2),rs.getString(3)) nrec = nrec + 1 rs.close() print >>self.file,"Time to get %d records: %4.4f"%(nrec, 1000*(time() - self.start)) lock.acquire() k.incr(nrec) lock.release() self.file.close() server, db,usr, passwd = "RAJDATTA", "test","rdatta", "rdatta" driver = "com.cxa.jdbc.Driver" #driver = "org.gjt.mm.mysql.Driver" forName(driver).newInstance() #url = "jdbc:mysql://%s/%s?user=%s&password=%s" % (server, db, usr, passwd) url = "jdbc:cxa:CXARAJ:tcp/P39D/8098" threadList = [] conList= [] connCount = int(sys.argv[1]) iter = int(sys.argv[2]) if (len(sys.argv) > 3): pollint = int(sys.argv[3]) else: pollint = 30 start = time() for i in xrange(0,connCount): conList.insert(i,DriverManager.getConnection(url)) print 'Total time to create %d connections: %.4f ms' % (connCount,1000*(time()-start)) #del threadList #threadList = [] lock = threading.Lock() start = time() k=0 kObj = keepCount(); print 'reccount %d'%(kObj.getreccount()) for j in range(connCount): k=k+1 threadList.append(getRows(k,conList[j],iter,kObj)) sleep(0.1) print "Total threads running: ",k threadList.append(doStats(k+1,pollint,kObj,connCount,iter)) for tTh in threadList: tTh.join() elapsed = time() - start print 'Total time to run %d queries: %.4f ms' % (k*iter,1000*elapsed) Thanks Raj Datta Professional Services Direct : 408-530-4932 CrossAccess Corp Cell : 408-316-5473 2900 Gordon Ave #100 Fax : 408-735-0328 Santa Clara CA 95051 Email : rd...@cr... |
From: brian z. <bz...@zi...> - 2002-01-17 04:10:32
|
Raj, Note: some lines wrapped so my line numbers might be off a bit > Sorry, I sent the wrong error message. Here is the correct one: > > I am getting the following namerror when I try to run the code below: > > Traceback (innermost last): > File "mtcxjdbcTester.py", line 31, in run > NameError: c You are trying to the instance variable 'c' to a local variable 'c' that is not previously either as a method argument or in the local block. You have already set the value of the instance variable 'c' in the constructor so it is unnecessary to do it again. For that matter, you don't need to do the first five assignments as they will all result in NameErrors, except of course for 'id' since this happens to be a Python builtin and the only reason line 30 didn't result in a NameError. > Traceback (innermost last): > File "mtcxjdbcTester.py", line 76, in run > AttributeError: 'int' object has no attribute 'incr' > > I'm not sure why line 50 didn't result in a NameError since the expression 'self.k = k' references a local variable 'k' which does not exist (AFAICT). The only reason 'self.iter' didn't raise an AttributeError is because 'iter' also happens to be a Python builtin. Nonetheless, k appears to be an integer as defined on my line 100 'k=0'. So when you get to line 76 (my line 75) it results in an AttributeError since int's have neither attributes or methods. Either you want 'k' to be of some other type which has an incr() method or you should change it to 'self.k += nrec'. I'm little confused as to how you wish to use the results of 'self.k += nrec' since you don't seem to reference them later (the instance variable that is). You will also run into errors in both run() methods because of similar issues. You do not need to reassign the instance variables since they were already assigned in the constructor. You will also need to be explicit about the referencing of instance variables such as on line 38. The variable 'pollint' does not exist in the local scope and I think you really wanted the instance variable 'self.pollint'. I think this chapter might help clear things up: http://www.python.org/doc/current/tut/node11.html. <shamless_plug> I also can't help but notice that you are doing JDBC work. I would recommend looking at zxJDBC (included in Jython2.1) as it does a lot of the heavy lifting for you, such as acquiring connections, iterating result sets and closing resources. It's also written to the DB API spec if cross-implementation development is of any interest. The documentation can be found at: http://jython.org/docs/zxjdbc.html. </shameless_plug> thanks, brian |