How to synchronize threads in python?

Posted by Eric on Stack Overflow See other posts from Stack Overflow or by Eric
Published on 2011-11-14T20:29:44Z Indexed on 2011/11/15 1:50 UTC
Read the original article Hit count: 249

I have two threads in python (2.7). I start them at the beginning of my program. While they execute, my program reaches the end and exits, killing both of my threads before waiting for resolution.

I'm trying to figure out how to wait for both threads to finish before exiting.

def connect_cam(ip, execute_lock):
    try:
        conn = TelnetConnection.TelnetClient(ip)
        execute_lock.acquire()
        ExecuteUpdate(conn, ip)
        execute_lock.release()
    except ValueError:
        pass


execute_lock = thread.allocate_lock()
thread.start_new_thread(connect_cam, ( headset_ip, execute_lock ) )
thread.start_new_thread(connect_cam, ( handcam_ip, execute_lock ) )

In .NET I would use something like WaitAll() but I haven't found the equivalent in python. In my scenario, TelnetClient is a long operation which may result in a failure after a timeout.

© Stack Overflow or respective owner

Related posts about python

Related posts about multithreading