不能很好地發揮我想爲一個文件名生成一個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