Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)
closed by

I've begun programming in Python half a month prior and was attempting to utilize Semaphores to synchronize two straightforward threads, for learning purposes. Here is the thing that 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's just printing 1's. Can anyone help me?

closed

4 Answers

0 votes
by (25.7k points)
 
Best answer

The reason you are only seeing "1" being printed is that both threads are acquiring the semaphore in sequence, and once the first thread acquires it, it releases it immediately, allowing the other thread to acquire it next. This creates a situation where the first thread repeatedly acquires and releases the semaphore before the second thread has a chance to acquire it. To synchronize the threads properly and alternate the printing of "1" and "2", you can modify your code as follows:

import threading sem = threading.Semaphore(0) # Initialize the semaphore with a value of 0 def fun1(): while True: print(1) sem.release() # Release the semaphore to allow the other thread to acquire it sem.acquire() # Acquire the semaphore to wait until the other thread releases it def fun2(): while True: sem.acquire() # Acquire the semaphore to wait until the other thread releases it print(2) sem.release() # Release the semaphore to allow the other thread to acquire it t = threading.Thread(target=fun1) t.start() t2 = threading.Thread(target=fun2) t2.start()

In this updated code, the semaphore is initially set to 0. The first thread (fun1) starts by printing "1" and then releases the semaphore, allowing the second thread (fun2) to acquire it. The second thread then prints "2" and releases the semaphore, allowing the first thread to acquire it again. This creates a back-and-forth synchronization between the two threads, printing "1" and "2" alternately.

0 votes
by (26.4k points)

It is turned out great, it's simply that it's printing excessively quickly for you to see. Have a go at putting a time.sleep() in the two functions (a limited quantity) to rest the string for that much measure of time, to really have the option to see both 1 just as 2.

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

Are you pretty much interested to learn python in detail? Come and join the python training course to gain more knowledge.

0 votes
by (15.4k points)
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()

t1 = threading.Thread(target=fun1)

t1.start()

t2 = threading.Thread(target=fun2)

t2.start()

However, the issue you're experiencing, where only "1" is being printed, is due to the threads acquiring and releasing the semaphore in quick succession. To ensure proper synchronization and alternate printing "1" and "2", you can modify the code as follows:

import threading

sem = threading.Semaphore(0)  # Initialize the semaphore with a value of 0

def fun1():

    while True:

        print(1)

        sem.release()  # Release the semaphore to allow the other thread to acquire it

        sem.acquire()  # Acquire the semaphore to wait until the other thread releases it

def fun2():

    while True:

        sem.acquire()  # Acquire the semaphore to wait until the other thread releases it

        print(2)

        sem.release()  # Release the semaphore to allow the other thread to acquire it

t1 = threading.Thread(target=fun1)

t1.start()

t2 = threading.Thread(target=fun2)

t2.start()

In this code, the semaphore is initially set to 0. The first thread (fun1) starts by printing "1" and then releases the semaphore, allowing the second thread (fun2) to acquire it. The second thread then prints "2" and releases the semaphore, allowing the first thread to acquire it again. This creates a synchronized behavior where "1" and "2" are printed alternately by the two threads.
0 votes
by (19k points)
import threading

sem = threading.Semaphore()

def print_numbers(num):

    while True:

        sem.acquire()

        print(num)

        sem.release()

t1 = threading.Thread(target=print_numbers, args=(1,))

t1.start()

t2 = threading.Thread(target=print_numbers, args=(2,))

t2.start()

In this version, the print_numbers function handles the printing of numbers. Each thread is created with a different number as an argument. The threads acquire the semaphore, print the number, and then release the semaphore. This ensures alternating printing of "1" and "2" between the two threads.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Sep 26, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer

Browse Categories

...