Python semaphores

I started programming in Python a few weeks ago and tried to use Semaphores to synchronize two simple threads for learning purposes. Here is what I have:

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 saves print only 1. How to move prints?

+14
source share
3 answers

It works great, its just that its printing is too fast for you. Try putting time.sleep()in both functions (a small amount) to sleep during this large amount of time, in order to actually be able to see both 1 and 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()
+11
source

You can also use the Lock / mutex method as follows:

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

/ :

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

[]:

, ,

+2

I used this code to demonstrate how 1 thread can use Semaphore and another thread will wait (without blocking) until Sempahore is available.

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

This will only work if synchronization is performed from one thread, IPC from separate processes will not work with 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 ran 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
0
source

Source: https://habr.com/ru/post/1598641/


All Articles