Created
December 2, 2020 10:36
-
-
Save silgon/d3fd03e4517bf998f933f536062276eb to your computer and use it in GitHub Desktop.
Example with lock thread
This file contains 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
""" Worker will wail to another to finish a task in order to do some procedure | |
""" | |
import threading | |
import logging | |
import time | |
logging.basicConfig(level=logging.DEBUG, | |
format='(%(threadName)-10s) %(message)s',) | |
def worker(lock, seconds=2): | |
logging.debug('Waiting for lock if it exists') | |
with lock: | |
logging.debug('Lock acquired') | |
time.sleep(seconds) | |
logging.debug('Lock freed') | |
if __name__ == '__main__': | |
lock = threading.Lock() | |
w1 = threading.Thread(target=worker, args=(lock, 2)) | |
w2 = threading.Thread(target=worker, args=(lock, 1)) | |
w1.start() | |
w2.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A link reference for the future