Douwe
Douwe

Reputation: 567

Non-blocking way to determine if thread is finished?

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"

How can I let the main thread know that the TestWorker has finished (call on_work_done()), without blocking calls to do_something_else() as thread.join() would?

Upvotes: 6

Views: 5248

Answers (3)

martineau
martineau

Reputation: 123393

You can give your thread instance an optional callback function to call when it's finished.
Note I added a Lock to prevent concurrent printing (which does block).

print_lock = threading.Lock()  # Prevent threads from printing at same time.

class TestWorker(threading.Thread):
    def __init__(self, threadID, name, callback=lambda: None):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.callback = callback

    def run(self):
        with print_lock:
            print("Starting " + self.name)
        time.sleep(3)
        with print_lock:
            print("Exiting " + self.name)
        self.callback()

class TestMain:
    def __init__(self):
        self.work_done = False

    def do_work(self):
        thread = TestWorker(1, "Thread-1", self.on_work_done)
        thread.start()

    def do_something_else(self):
        with print_lock:
            print("Something else")

    def on_work_done(self):
        with print_lock:
            print("work done")
        self.work_done = True

main = TestMain()
main.do_work()
while not main.work_done:
    main.do_something_else()
    time.sleep(.5)  # do other stuff...

print('Done')

Output:

Starting Thread-1
Something else
Something else
Something else
Something else
Something else
Something else
Exiting Thread-1
work done
Done

Upvotes: 8

vadim vaduxa
vadim vaduxa

Reputation: 221

import queue
import threading

class SThread(threading.Thread, queue.Queue):
    def __init__(self, queue_out: object):
        threading.Thread.__init__(self)
        queue.Queue.__init__(self)
        self.queue_out = queue_out
        self.setDaemon(True)
        self.start()

    def run(self):
        print('Thread start')
        while True:
            cmd = self.get()
            if cmd is None:
                break  # exit thread
            self.queue_out.put(cmd['target'](*cmd.get('args', ())), **cmd.get('kwargs', {}))
            self.task_done()
        print('Thread stop')

def testFn(a):
    print('+ %s' % a)
    return a

if __name__ == '__main__':
    print('main 1')
    # init
    queue_out = queue.Queue()
    thread = SThread(queue_out)

    # in
    for a in range(5): thread.put(dict(target=testFn, args=(a,)))

    thread.put(None)
    print('main 2')
    # out
    while True:
        try:
            print('- %s' % queue_out.get(timeout=3))
        except queue.Empty:
            break
    print('main 3')

OUT:

main 1
Thread start
main 2
+ 0
+ 1
+ 2
+ 3
+ 4
Thread stop
- 0
- 1
- 2
- 3
- 4
main 3

Upvotes: 1

vadim vaduxa
vadim vaduxa

Reputation: 221

import threading
dt = {}
threading.Thread(target=dt.update, kwargs=dict(out=123)).start()

while 'out' not in dt:
    print('out' in dt)
print(dt)

Upvotes: 0

Related Questions