不能很好地发挥我想为一个文件名生成一个uuid,并且我也使用了多处理模块。不愉快的是,我所有的uuids都完全一样。这里是一个小例子:Python多处理与uuid.uuid4()
import multiprocessing
import uuid
def get_uuid(a):
## Doesn't help to cycle through a bunch.
#for i in xrange(10): uuid.uuid4()
## Doesn't help to reload the module.
#reload(uuid)
## Doesn't help to load it at the last minute.
## (I simultaneously comment out the module-level import).
#import uuid
## uuid1() does work, but it differs only in the first 8 characters and includes identifying information about the computer.
#return uuid.uuid1()
return uuid.uuid4()
def main():
pool = multiprocessing.Pool(20)
uuids = pool.map(get_uuid, range(20))
for id in uuids: print id
if __name__ == '__main__': main()
我偷看到uuid.py的代码,它似乎取决于 - 上的平台使用一些操作系统级例程的随机性,所以我难倒作为一个python级解决方案(做类似于重新加载uuid模块或选择新的随机种子)。我可以使用uuid.uuid1(),但只有8位数字不同,我认为这是从时间推导出来的,这似乎很危险,尤其是考虑到我是多处理器(所以代码可能在同一时间执行) 。有关于这个问题的智慧吗?
果然,设置uuid._uuid_generate_random = None是正确的。必须是Mac OS X的libc uuid_generate_random()中的错误。感谢您提交错误报告的建议:http://bugs.python.org/issue8621 – yig 2010-05-05 12:01:41
我还针对Mac OS X提交了一个平台错误:http://openradar.appspot.com/radar?id=334401 – yig 2010-05-05 14:19:45