我正在尝试为文件名生成UUID,并且我还在使用多进程模块。不幸的是,我的所有UUID最终都完全相同。这里有一个小例子:
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位数字不同,并且我认为这些数字仅来源于时间,这似乎很危险,特别是考虑到我正在进行多进程处理(因此代码可能在完全相同的时间执行)。是否有关于这个问题的智慧?