An error in threading.py?

O

Oltmans

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"]="(e-mail address removed)"
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)
 
R

Robert Kern

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)

Why omit the imports?
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

The problem is in tmechanize.py, not threading.py. You forgot to actually make
the lock object.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
F

Falcolas

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

Time for the obvious question - where do you define "lock"? If you're
trying to put a mutex around your increment, you need a lock pre-
defined to use it. So, you'll need to add something like "lock = Lock
()" near the top, with a "global lock" before you try using it in your
with statement.

If you try using Lock() directly in the thread, you'll create a new
lock per thread, thus defeating the purpose of trying to lock in the
first place.

~G
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,169
Latest member
ArturoOlne
Top