semaphores and Rlocks
"Martin v. Löwis"
martin at v.loewis.de
Fri Dec 20 06:12:57 EST 2002
Jive Dadson wrote:
> Now I am even confuseder than I was. I found threading.py, which looks like it
> implements semaphores and condition variables using polling and sleeping. Yes? No?
Yes on condition variables, if they have a timeout. No on semaphores and
condition variables without timeout.
> But I also found thread.c in the C sources, which appears to define semaphores
> and locks by means of the MS Windows API.
Where did you find this? thread_nt.h is what is used on Windows, and it
uses Win32 semaphores only in thread creation. thread.lock is
implemented using
InterlockedCompareExchange/InterlockedIncrement/WaitForsingleObject.
> If I had Python locks (like CRITICAL_SECTION in win32 land)
> and true non-polling semaphores (like Win32 CreateSemaphore, WaitForSingleObject,
> and ReleaseSemaphore), I could roll my own condition variables portably in Python.
In threading.py, the relationship is reverse: semaphores are implemented
using condition variables, not vice versa. They are non-polling, as they
don't give a timeout to the condition variable.
> But I look in the thread module per se and all I find is allocate_lock,
> no semaphores.
Correct.
> It takes a "LockType" parameter
Where did you find this? All allocate-lock functions I could find take
no arguments; the thread.allocate_lock Python function returns an
instance of thread.LockType.
> I also found a module called "mutex".
See http://www.python.org/doc/current/lib/module-mutex.html. It's meant
for use with sched.py, i.e. isn't directly related to threading.
> I don't want to do a C extension, in part because I don't currently
> have access to UNIX or Mac development systems, but I would probably
> do that rather than abandoning the idea of doing this project in Python.
The thread module is purposefully limited in the primitives it provides
primarily to be able to provide it on all systems. On top of these
primitives, more elaborate synchronization objects are implemented
in threading.py.
If you want threading objects implemented directly using the Win32 API,
see http://python.org/sf/500447. If you find the time to revise this
patch so that it transparently get those primitives instead of the
pure-Python implementations on Windows, please do contribute.
Regards,
Martin
More information about the Python-list
mailing list