Created
August 1, 2018 18:39
-
-
Save skeller88/4b38a348ca43be8f7dcb78ed11326690 to your computer and use it in GitHub Desktop.
python multithreading lock acquire and release
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if __name__ == '__main__': | |
lock = threading.Lock() | |
def watch(num): | |
while True: | |
sleep(random.randint(0,5)) | |
acquired = lock.acquire(blocking=False) | |
if acquired: | |
print('\n') | |
print(num, 'acquired lock') | |
sleep(random.randint(0,5)) | |
lock.release() | |
else: | |
print(num, 'did not acquire lock') | |
sleep(random.randint(0,5)) | |
with ThreadPoolExecutor(max_workers=10) as ex: | |
for worker in range(5): | |
ex.submit(watch, worker) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment