Sebastian Noack added the comment:

I was just waiting for a comment pointing out, that my patch comes without 
tests. :) Note that we are still discussing the implementation and this patch 
is just a proof of concept. And since the way it is implemented and the API it 
provides could still change, its quite pointless to write tests, until we at 
least agreed on the API.

I have uploaded a new patch. The way it is implemented now, is more like the 
Barrier is implemented. The common code is shared in the threading module and 
the shared/exclusive lock objects can be pickled now. I have also fixed a bug 
related to acquiring locks in non-blocking mode.

However the code still uses c_uint, but ctypes (and 
multiprocessing.sharedtypes) is only imported when ShrdExclLock is called. So 
it is just a lazy dependency, now. However the reason why I am using ctypes 
instead of python integers for threading and a BufferWrapper for 
multiprocessing (as the Barrier does) is, because of 2 of the 4 counters need 
to be continuously incremented, and c_uint has the nice feature that it can 
overflow, in contrast to python integers and integers in arrays. Also that way 
the implementation is simpler and it seems that there isn't much difference 
under the hood between using BufferWrapper() and RawValue().

A shared/exclusive lock isn't one lock but two locks, which are synchronized, 
but must be acquired separately. Similar to a pipe, which isn't one file, but 
one file connected to another file that reads whatever you have written into 
the first file. So it isn't strange to create two lock objects, as it also 
isn't strange that os.pipe() returns two file descriptors.

Also having a separate lock object for the shared and exclusive lock, each 
providing the same API (as Lock and RLock), gives you huge flexibility. You can 
acquire both locks using the with statement or pass them separately around. So 
for example when you have a function, thread or child process, that should only 
be able to acquire either the shared or the exclusive lock, you don't have to 
pass both locks. That also means that existing code that expects a lock-like 
object will be compatible with both the shared and exclusive lock.

----------
Added file: 
http://bugs.python.org/file27363/Added-ShrdExclLock-to-threading-and-multiprocessing-2.patch

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue8800>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to