issue #493: less CPU-intensive cookie format.

issue510
David Wilson 6 years ago
parent 14d393765b
commit bc84d1e950

@ -32,6 +32,7 @@ non-essential code in order to reduce its size, since it is also serves as the
bootstrap implementation sent to every new slave context. bootstrap implementation sent to every new slave context.
""" """
import binascii
import collections import collections
import encodings.latin_1 import encodings.latin_1
import errno import errno
@ -2093,16 +2094,18 @@ class Latch(object):
self._cls_all_sockets.extend((rsock, wsock)) self._cls_all_sockets.extend((rsock, wsock))
return rsock, wsock return rsock, wsock
COOKIE_MAGIC, = struct.unpack('L', 'LTCH' * (struct.calcsize('L')/4))
COOKIE_FMT = 'Llll'
COOKIE_SIZE = struct.calcsize(COOKIE_FMT)
def _make_cookie(self): def _make_cookie(self):
""" """
Return a string encoding the ID of the instance and the current thread. Return a string encoding the ID of the process, instance and thread.
This disambiguates legitimate wake-ups, accidental writes to the FD, This disambiguates legitimate wake-ups, accidental writes to the FD,
and buggy internal FD sharing. and buggy internal FD sharing.
""" """
ident = thread.get_ident() return struct.pack(self.COOKIE_FMT, self.COOKIE_MAGIC,
return b(u'%010d-%016x-%016x' % (os.getpid(), int(id(self)), ident)) os.getpid(), id(self), thread.get_ident())
COOKIE_SIZE = len(_make_cookie(None))
def get(self, timeout=None, block=True): def get(self, timeout=None, block=True):
""" """
@ -2178,7 +2181,8 @@ class Latch(object):
assert cookie == got_cookie, ( assert cookie == got_cookie, (
"Cookie incorrect; got %r, expected %r" \ "Cookie incorrect; got %r, expected %r" \
% (got_cookie, cookie) % (binascii.hexlify(got_cookie),
binascii.hexlify(cookie))
) )
assert i < self._waking, ( assert i < self._waking, (
"Cookie correct, but no queue element assigned." "Cookie correct, but no queue element assigned."

Loading…
Cancel
Save