Semaphores on Python

2024/11/19 23:13:24

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?

Answer

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()
https://en.xdnf.cn/q/26380.html

Related Q&A

Overload () operator in Python

I am trying to learn currying in Python for my class and I have to overload the () operator for it. However, I do not understand how can I can go about overloading the () operator. Can you explain the …

Python pandas apply function if a column value is not NULL

I have a dataframe (in Python 2.7, pandas 0.15.0):df=A B C 0 NaN 11 NaN 1 two NaN [foo, bar] 2 three 33 NaNI want to apply a simple function for ro…

python pip trouble installing from requirements.txt

Ive had great luck with pip in the past, but working at installing some stuff in a venv on is giving me some headaches. I keep getting errors likeNo distributions at all found for somepackage Storing d…

Using py.test with coverage doesnt include imports

For Jedi we want to generate our test coverage. There is a related question in stackoverflow, but it didnt help.Were using py.test as a test runner. However, we are unable to add the imports and other…

Determine if a Python class is an Abstract Base Class or Concrete

My Python application contains many abstract classes and implementations. For example:import abc import datetimeclass MessageDisplay(object):__metaclass__ = abc.ABCMeta@abc.abstractpropertydef display(…

Pandas printing ALL dtypes

This seems like a very simple problem, however its driving me round the bend. Im sure it should be solved by RTFM, but Ive looked at the options and I can see the one to fix it.I just want to print the…

Which Python API should be used with Mongo DB and Django

I have been going back and forth over which Python API to use when interacting with Mongo. I did a quick survey of the landscape and identified three leading candidates.PyMongo MongoEngine MingIf you w…

Save a list to a .txt file

Is there a function in python that allows us to save a list in a txt file and keep its format?If I have the list:values = [1,2,3]can I save it to a file that contains:[1,2,3]So far I print parts of th…

How to run a python script like pm2 for nodejs

Ive used pm2 for my Node.js script and I love it. Now I have a python script which collect streaming data on EC2. Sometimes the script bombs out and I would like a process manager to restart itself lik…

Resize rectangular image to square, keeping ratio and fill background with black

Im trying to resize a batch of grayscale images that are 256 x N pixels (N varies, but is always ≤256). My intention is to downscale the images.The resize would have to output a square (1:1) image, wi…