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?
one = test('one', 1).start()
two = test('two', 3).start()
Your problem is that start()
doesn't do a return self
. one
and two
are not threads. They're None
or whatever the return value of start()
actually is.
This works:
one = test('one', 1)
one.start()
two = test('two', 3)
two.start()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.