'Test for a thread lock gets stuck on thread.join()
I was writing my own implementation for Memcachelock in my python project, but got stucked on testing it out. My code:
if __name__ == "__main__":
from threading import Thread
# demo of the lock in use
def mock_process_image(idx):
with ImageLock("image-name"):
print("process %d: processing image" % idx)
time.sleep(3) # seconds of "processing"
print("process %d: completed processing of the image" % idx)
threads = []
for idx in range(3): # count of parallel processing
thread = Thread(target=mock_process_image, args=(idx,))
thread.start()
threads.append(thread)
for thread in threads:
thread.join()
With ImageLock defined as:
class ImageLock(MemcacheLock):
"""Preconfigured memcached lock for images."""
def __init__(self, image: str) -> None:
"""
Initialize image lock.
Args:
image (str): image name (unique string)
"""
super().__init__(
image,
key_prefix="image:",
expiration=(10 * MINUTE),
timeout=(11 * MINUTE),
sleep_interval=(1 * SECOND),
)
I was trying to test if the threads get executed in sequence as expected, but instead I am unable to get any output from mock_process_image, it seems like the threading gets stuck at first thread.join() and I am unable to debug it or figure out what's wrong. Anyone who could help me out?
Edit: After reducing the expiration/timeout time as Nullman sugested I am getting these prints:
process 0: processing image
Lock timeouts: repository:repository-name
Lock timeouts: repository:repository-name
process 1: processing image
process 2: processing image
process 0: completed processing of the image
process 1: completed processing of the image
process 2: completed processing of the image ```
Which I suppose means that the threads are not locked properly?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
