3

uuid在 Python 中,我可以始终在模块中生成段错误。这可以通过uuid.uuid1()从多个线程重复调用来完成。经过一番挖掘,这个函数似乎最终uuid_generate_time通过以下方式调用了 C 函数ctypes

来自 uuid.py:

for libname in ['uuid', 'c']:
    try:
        lib = ctypes.CDLL(ctypes.util.find_library(libname))
    except:
        continue
    if hasattr(lib, 'uuid_generate_random'):
        _uuid_generate_random = lib.uuid_generate_random
    if hasattr(lib, 'uuid_generate_time'):
        _uuid_generate_time = lib.uuid_generate_time
        if _uuid_generate_random is not None:
            break  # found everything we were looking for

稍后在 的定义中uuid1()

def uuid1(node=None, clock_seq=None):
    """Generate a UUID from a host ID, sequence number, and the current time.
    If 'node' is not given, getnode() is used to obtain the hardware
    address.  If 'clock_seq' is given, it is used as the sequence number;
    otherwise a random 14-bit sequence number is chosen."""

    # When the system provides a version-1 UUID generator, use it (but don't
    # use UuidCreate here because its UUIDs don't conform to RFC 4122).
    if _uuid_generate_time and node is clock_seq is None:
        _buffer = ctypes.create_string_buffer(16)
        _uuid_generate_time(_buffer)
        return UUID(bytes=_buffer.raw)

我已经阅读了手册页uuid_generate_time以及 Python 文档uuid.uuid1,但没有提到线程安全。我认为这与它需要访问系统时钟和/或 MAC 地址有关,但这只是一个盲目的猜测。

我想知道是否有人可以启发我?

以下是我用来生成 seg 错误的代码:

import threading, uuid

# XXX If I use a lock, I can avoid the seg fault
#uuid_lock = threading.Lock()
def test_uuid(test_func):
  for i in xrange(100):
    test_func()
    #with uuid_lock:
    #  test_func()

def test(test_func, threads):
  print 'Running %s with %s threads...' % (test_func.__name__, threads)

  workers = [threading.Thread(target=test_uuid, args=(test_func,)) for x in xrange(threads)]
  [x.start() for x in workers]
  [x.join() for x in workers]
  print 'Done!'

if __name__ == '__main__':
  test(uuid.uuid4, 8)
  test(uuid.uuid1, 8)

我得到的输出是:

Running uuid4 with 8 threads...
Done!
Running uuid1 with 8 threads...
Segmentation Fault (core dumped)

哦,我在 Solaris 上运行它...

4

1 回答 1

0

文档没有说它是线程安全的,所以你不能假设它是线程安全的。就这么简单。

查看当前的OpenIndiana 源代码并不uuid_generate_time完全清楚导致段错误的原因。然而,虽然该函数确实使用了锁,但它在执行许多初始化任务时并不持有该锁。这可能与问题有关,但我无法指出竞争条件会导致故障的特定位置。您可以uuid1在启动任何线程之前尝试调用一次,看看这是否会使问题消失。虽然最好只使用自己的锁,因为不能保证 Python 代码本身是线程安全的。

于 2014-07-29T04:46:02.880 回答