import threading sem = threading.Semaphore() # or sem = threading.Semaphore(value=n) sem.acquire() # Your code here sem.release()