single thread decrement a semaphore object more than once?

B

birdsong

I searched but didn't see this already discussed, sorry if I didn't
search hard enough.

Can I decrement a semaphore's counter within the same thread more than
once? I'd like to set hard and soft limits in a server I'm writing.
The server upon initialization would create a semaphore:

threading.Semaphore(100)

Then acquire 50x to hold half of the locks before serving any
requests. This allows me to grow the number of locks to the hard
limit via signals or some other mechanism, but I need to know that any
1 thread has the ability to acquire up or release more than 1x and
effectively acquire and release for that number of semaphore values.

I could implement this by using regular threading.Lock in a pool, but
the Semaphore object would be much easier.
 
B

birdsong

Yes, you are not restricted to multiple threads. A semaphore ensures
that the counter is thread safe. It doesn't force you to use threads.

Christian

ahh good. there will be multiple threads accessing, but yes i follow
now. i guess this would have been pretty easy to test in the shell.

muchas gracias.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top