2

Python threading module do not realize true parallel programming. Threads were switched one another. Then, is Semaphore necessary? Semaphore is for sending signal to other threads that should be processed at the same time. I think it is only useful in multiprocessing. Why is the Semaphore Class in threading module?

1 Answer 1

1

Because you can't be sure, when scheduler will switch threads. Consider SMP machine. It will always process one thread at a time. But nevertheless multithreaded code should use semaphores, mutexes, critical sections and other synchronization primitives.

Although CPython have GIL and can't execute more then one thread at a time, there can be other implementation without such a limitation. Also, even with GIL present, from thread POV all your threads are running simultaneously. You can't write piece of code that will prove opposite (yes, you can ask OS about this, but it is a cheating).

Strictly speaking, semaphore is used not for sending signals to other threads, but to protect access to a limited number of resources. For example, you can have 5 printers and 10 threads that want to print. You will have one Semaphore initialized with value 5 and it will protect access to your printers. So at any moment no more than 5 threads will print something.

Another example is connection pool. For example you have pool of N connections to DB. This means that no more than N threads can simultaneously execute queries to DB. In this case you will have Semaphore initialized with N. (N+1)'th thread will block on Semaphore and will wait for free connection.

Sign up to request clarification or add additional context in comments.

1 Comment

Comments are not for extended discussion; this conversation has been moved to chat.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.