Synchronizing and Locking Threads
Subscribe to Tech with Tim
Synchronizing and Locking Threads
This python multi-threading tutorial will cover how to synchronize and lock threads. This essentially means waiting for a specific thread to finish running before any other can go. An example of when you would want to do this is the following. Imagine an online payment checkout, some tasks that need to be completed during checkout are the following: – Verifying Payment/Card Details – Sending Confirmation email or shipping details – Loading a thank you page or redirecting back to main website For the purpose of our example we will assume each of these will be their own threads. Now the problem arises that if for some reason the payment is stalling our other threads will take over and run. This is problematic because we can’t send a confirmation email until we know the payment cleared. So we need to find a way to ensure that our other threads don’t run until the payment clears. That is why we use locking.
Example 1
This example shows a very basic example of locking, where we use two threads and thread one cannot go until thread two goes. Try running it and seeing the output for yourself.
# EXAMPLE 1 import threading import time class myThread(threading.Thread): def __init__(self, threadId, name, count): threading.Thread.__init__(self) self.threadId = threadId self.name = name self.count = count def run(self): print("Starting: " + self.name + "\n") threadLock.acquire() print_time(self.name, 1,self.count) threadLock.release() print("Exiting: " + self.name + "\n") def print_time(name, delay, count): while count: time.sleep(delay) print ("%s: %s %s" % (name, time.ctime(time.time()), count) + "\n") count -= 1 threadLock = threading.Lock() thread1 = myThread(1, "Thread 1", 5) thread2 = myThread(2, "Thread 2", 5) thread1.start() thread2.start() thread1.join() thread2.join() print("Done main thread")
Example 2
This next example simulates the running of the checkout system defined above. Where after the payment has cleared either thread can run and they can continue to switch back and forth if one is waiting for something to occur.
# EXAMPLE 2 import threading import time class myThread(threading.Thread): def __init__(self, threadId, name, count): threading.Thread.__init__(self) self.threadId = threadId self.name = name self.count = count def run(self): print("Starting: " + self.name + "\n") threadLock.acquire() print_time(self.name, 1,self.count) threadLock.release() print("Exiting: " + self.name + "\n") class myThread2(threading.Thread): def __init__(self, threadId, name, count): threading.Thread.__init__(self) self.threadId = threadId self.name = name self.count = count def run(self): print("Starting: " + self.name + "\n") threadLock.acquire() threadLock.release() print_time(self.name, 1,self.count) print("Exiting: " + self.name + "\n") def print_time(name, delay, count): while count: time.sleep(delay) print ("%s: %s %s" % (name, time.ctime(time.time()), count) + "\n") count -= 1 threadLock = threading.Lock() thread1 = myThread(1, "Payment", 5) thread2 = myThread2(2, "Sending Email", 10) thread3 = myThread2(3, "Loading Page", 3) thread1.start() thread2.start() thread3.start() thread1.join() thread2.join() thread3.join() print("Done main thread")