An error in threading.py?
Oltmans
rolf.oltmans at gmail.com
Wed Mar 11 18:15:28 EDT 2009
Hi, all. I'm trying to use Mechanize in a multithreaded program--
purpose of which is to fill out a form on a website using concurrent
threads. Guys, trust me I've spent a lot of time to figure out the
problem but I'm completed puzzled. Firstly, I've listed the errors and
then the program listing (with imports omitted)
Error:
Traceback (most recent call last):
File "C:\Python25\lib\threading.py", line 460, in __bootstrap
self.run()
File "tmechanize.py", line 21, in run
with lock:
NameError: global name 'lock' is not defined
Program:
-------------
#!/usr/bin/env python
requestNumber=0
class Requests(Thread):
def __init__(self, times):
Thread.__init__(self)
self.times=times
self.html=' '
self.requestTime={}
self.starttime=0
self.endtime=0
self.br= Browser()
def run(self):
for i in range(0,self.times):
self.starttime=time.clock()
self.SendRequest()
self.endtime=time.clock()
with lock:
global requestNumber
requestNumber += 1
print 'locking the time....'
self.requestTime[requestNumber]=self.endtime -
self.starttime
def SendRequest(self): #A class method
# it sends a request to website using mechanize library
self.br.add_password("https://example.com/admin", "admin",
"admin")
res=self.br.open("https://example.com/admin")
print 'Successfully loggedin '
self.html=res.read()
print 'Filling in the form'
self.br.select_form(name="formOne")
self.br["textbox"]="www.google.com"
self.br["textBox1"]='www.example.com'
self.br["users[0].firstName"]="firstName"
self.br["users[0].lastName"]="LastName"
self.br["users[0].emailAddress"]="fname at example.com"
print 'Submitting the form'
resp=self.br.submit()
self.html=resp.read()
def startThis(times,reqs):
#print 'Threads ='+str(times)
#print 'Reques/Thread ='+ str(maxReqs)
threads=[]
for i in range (0,reqs):
owner=Requests(times)
owner.start()
threads.append(owner)
for thread in threads:
thread.join()
if __name__=="__main__":
#I want to create 2 threads, each of them will execute twice. At
least that is the intention.
startThis(2,2)
More information about the Python-list
mailing list