Skip to content

Instantly share code, notes, and snippets.

View schedutron's full-sized avatar
🎯
Focusing

Saurabh Chaturvedi schedutron

🎯
Focusing
View GitHub Profile
@schedutron
schedutron / rlock_tut.py
Last active August 13, 2017 03:11
A demo script for threading.RLock
import threading
num = 0
lock = Threading.Lock()
lock.acquire()
num += 1
lock.acquire() # This will block.
num += 2
lock.release()
@schedutron
schedutron / semaphore_tut.py
Last active August 13, 2017 03:14
A demo script for threading module's semaphores
import random, time
from threading import BoundedSemaphore, Thread
max_items = 5
container = BoundedSemaphore(max_items) # consider this as a container with a capacity of 5 items.
  # Defaults to 1 if nothing is passed.
def producer(nloops):
for i in range(nloops):
time.sleep(random.randrange(2, 5))
@schedutron
schedutron / lock_tut.py
Last active August 13, 2017 03:15
A demo script for threading.Lock
from threading import Lock, Thread
lock = Lock()
g = 0
def add_one():
global g # Just used for demonstration. It’s bad to use the ‘global’ statement in general.
lock.acquire()
g += 1
lock.release()