<p>我试图为文件名生成一个uuid,我还使用了多处理模块。令人不快的是,我所有的uuid最终都是一样的。下面是一个小例子:</p>
<pre><code>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()
</code></pre>
<p>我偷看了一下uuid.py的代码,而且它似乎依赖于平台使用了一些操作系统级的例程来实现随机性,所以对于python级别的解决方案(如重新加载uuid模块或选择一个新的随机种子)我感到困惑。我可以利用uuid.uuid1(),但只有8个数字不同,而且我认为这是从时间派生出来的,这似乎很危险,尤其是考虑到我正在进行多处理(因此代码可以完全在同一时间执行)。在这个问题上有什么智慧吗?在</p>