I have the following code:
import threading
import time
class TestWorker (threading.Thread):
def __init__(self, threadID, name):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
def run(self):
print "Starting " + self.name
time.sleep(20)
print "Exiting " + self.name
# how do I let the calling thread know it's done?
class TestMain:
def __init__(self):
pass
def do_work(self):
thread = TestWorker(1, "Thread-1")
thread.start()
def do_something_else(self):
print "Something else"
def on_work_done(self):
print "work done"
如何让主线程知道TestWorker
已经完成(调用on_work_done()
),而不会像thread.join()
一样阻塞对do_something_else()
的调用?
TestWorker.run
中调用TestMain.on_work_done()
。 - Aran-Fey