Semaphores on Python

好久不见. 提交于 2019-11-29 01:41:08

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()

You can also use Lock/mutex method as following:

import threading
import time

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


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


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

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

Another/Simpler usage type:

import threading
import time

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


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


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

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

Note: additionaly difference between mutex and semaphore, mutex VS semaphore

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!