Semaphores on Python

67,076

Solution 1

It is working fine, its just that its printing too fast for you to see . Try putting a time.sleep() in both functions (a small amount) to sleep the thread for that much amount of time, to actually be able to see both 1 as well as 2.

Example -

import threading
import time
sem = threading.Semaphore()

def fun1():
    while True:
        sem.acquire()
        print(1)
        sem.release()
        time.sleep(0.25)

def fun2():
    while True:
        sem.acquire()
        print(2)
        sem.release()
        time.sleep(0.25)

t = threading.Thread(target = fun1)
t.start()
t2 = threading.Thread(target = fun2)
t2.start()

Solution 2

Also, you can use Lock/mutex method as follows:

import threading
import time

mutex = threading.Lock()  # is equal to threading.Semaphore(1)

def fun1():
    while True:
        mutex.acquire()
        print(1)
        mutex.release()
        time.sleep(.5)

def fun2():
    while True:
        mutex.acquire()
        print(2)
        mutex.release()
        time.sleep(.5)

t1 = threading.Thread(target=fun1).start()
t2 = threading.Thread(target=fun2).start()

Simpler style using "with":

import threading
import time

mutex = threading.Lock()  # is equal to threading.Semaphore(1)

def fun1():
    while True:
        with mutex:
            print(1)
        time.sleep(.5)

def fun2():
    while True:
        with mutex:
            print(2)
        time.sleep(.5)

t1 = threading.Thread(target=fun1).start()
t2 = threading.Thread(target=fun2).start()

[NOTE]:

The difference between mutex, semaphore, and lock

Solution 3

In fact, I want to find asyncio.Semaphores, not threading.Semaphore, and I believe someone may want it too.

So, I decided to share the asyncio.Semaphores, hope you don't mind.

from asyncio import (
    Task,
    Semaphore,
)
import asyncio
from typing import List


async def shopping(sem: Semaphore):
    while True:
        async with sem:
            print(shopping.__name__)
        await asyncio.sleep(0.25)  # Transfer control to the loop, and it will assign another job (is idle) to run.


async def coding(sem: Semaphore):
    while True:
        async with sem:
            print(coding.__name__)
        await asyncio.sleep(0.25)


async def main():
    sem = Semaphore(value=1)
    list_task: List[Task] = [asyncio.create_task(_coroutine(sem)) for _coroutine in (shopping, coding)]
    """ 
    # Normally, we will wait until all the task has done, but that is impossible in your case.
    for task in list_task:
        await task
    """
    await asyncio.sleep(2)  # So, I let the main loop wait for 2 seconds, then close the program.


asyncio.run(main())

output

shopping
coding
shopping
coding
shopping
coding
shopping
coding
shopping
coding
shopping
coding
shopping
coding
shopping
coding

16*0.25 = 2

Solution 4

I used this code to demonstrate how 1 thread can use a Semaphore and the other thread will wait (non-blocking) until the Sempahore is available.

This was written using Python3.6; Not tested on any other version.

This will only work is the synchronization is being done from the same thread, IPC from separate processes will fail using this mechanism.

import threading
from  time import sleep
sem = threading.Semaphore()

def fun1():
    print("fun1 starting")
    sem.acquire()
    for loop in range(1,5):
        print("Fun1 Working {}".format(loop))
        sleep(1)
    sem.release()
    print("fun1 finished")



def fun2():
    print("fun2 starting")
    while not sem.acquire(blocking=False):
        print("Fun2 No Semaphore available")
        sleep(1)
    else:
        print("Got Semphore")
        for loop in range(1, 5):
            print("Fun2 Working {}".format(loop))
            sleep(1)
    sem.release()

t1 = threading.Thread(target = fun1)
t2 = threading.Thread(target = fun2)
t1.start()
t2.start()
t1.join()
t2.join()
print("All Threads done Exiting")

When I run this - I get the following output.

fun1 starting
Fun1 Working 1
fun2 starting
Fun2 No Semaphore available
Fun1 Working 2
Fun2 No Semaphore available
Fun1 Working 3
Fun2 No Semaphore available
Fun1 Working 4
Fun2 No Semaphore available
fun1 finished
Got Semphore
Fun2 Working 1
Fun2 Working 2
Fun2 Working 3
Fun2 Working 4
All Threads done Exiting
Share:
67,076
Victor Turrisi
Author by

Victor Turrisi

Computer Scient student, trying to master the arts of Java and Python programming.

Updated on July 09, 2022

Comments

  • Victor Turrisi
    Victor Turrisi almost 2 years

    I've started programming in Python a few weeks ago and was trying to use Semaphores to synchronize two simple threads, for learning purposes. Here is what I've got:

    import threading
    sem = threading.Semaphore()
    
    def fun1():
        while True:
            sem.acquire()
            print(1)
            sem.release()
    
    def fun2():
        while True:
            sem.acquire()
            print(2)
            sem.release()
    
    t = threading.Thread(target = fun1)
    t.start()
    t2 = threading.Thread(target = fun2)
    t2.start()
    

    But it keeps printing just 1's. How can I intercale the prints?

  • Victor Turrisi
    Victor Turrisi almost 9 years
    Thx for the help, but I found out the real problem, since I'm using the same semaphore in both threads the first one is finishing almost instantly so the second cannot get the lock and execute.
  • Anand S Kumar
    Anand S Kumar almost 9 years
    @VictorTurrisi Instead of while True if you put a big range and run your program, and then redirect the output to a file and then check the file, you may be able to see that 2 does get printed in between but its like , lots of 1 and then lots of 2 then again lots of 1, etc. This is because it is executing too fast and you need to put a time.sleep() between them to see them executing one after the other.