Skip to content

Instantly share code, notes, and snippets.

@silgon
Created December 2, 2020 10:36
Show Gist options
  • Save silgon/d3fd03e4517bf998f933f536062276eb to your computer and use it in GitHub Desktop.
Save silgon/d3fd03e4517bf998f933f536062276eb to your computer and use it in GitHub Desktop.
Example with lock thread
""" 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()
@silgon
Copy link
Author

silgon commented Dec 2, 2020

A link reference for the future

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment