import threading, time
class test(threading.Thread):
def __init__(self,name,delay):
threading.Thread.__init__(self)
self.name = name
self.delay = delay
def run(self):
c = 0
while True:
time.sleep(self.delay)
print 'This is thread %s on line %s' %(self.name,c)
c = c + 1
if c == 15:
print 'End of thread %s' % self.name
break
one = test('one', 1).start()
two = test('two', 3).start()
one.join()
two.join()
print 'End of main'
Problem: cannot get join() methods to work properly, gives the following error:
Traceback (most recent call last)line 29, in <module> join() NameError: name 'join' is not defined
if i remove:
one.join
two.join
the code works perfectly fine.
I wanted to print the last line,
print 'End of main'
after the two threads have ended. I can’t seem to understand why join() is not an attribute of the two instances?
Your problem is that
start()doesn’t do areturn self.oneandtwoare not threads. They’reNoneor whatever the return value ofstart()actually is.This works: