6

数百メガバイトの大きな辞書があり、それをディスク上の棚に作りたいとしましょう。pyparを使用してMPIを使用し、マスターリストのクリーンなビットを生成しています。これを達成するための最良の方法は何ですか?例:

# much earlier
masterDict = shelve.open( 'aShelveFile' )
# .. . . . . 
# then we work out which parts of masterDict to keep
# and we put into aCleanDict
# then use mpi pypar to send the cleaned bits to the master rank
if pypar.rank() == 0:
  tempdict = {}
  for p in range(1,pypar.size()):
    tempdict.append(pypar.receive(p))
  for l1 in tempdict:
    for l2 in l1:
      realDict.append(l2)
  for p in range(1,pypar.size()):
    pypar.send(realDict,p)

  # now realDict has all the cleaned bits
  # which we send to the other hosts
else:
  pypar.send(aCleanDict, 0 )
  aCleanDict = pypar.receive( 0 )

# now to replace masterDict  with aCleanDict
# note: cannot send shelve dictonaries using pypar

# insert stackover flow code here.
4

1 に答える 1

7

ここでは、簡単な辞書を棚上げし、キーを介してアクセスできるようにしますmyDict

import shelve
myDict = {"a" : 1, "b" : 2}
myShelvedDict = shelve.open("my_shelved_dictionary.db")
myShelvedDict["myDict"] = myDict

棚上げされるものはすべて、辞書の内容がピクルス可能でなければならないことに注意してください。

シェルフ内の辞書の構造を複製する場合、つまり、キーを持たずにmyDict辞書のキーをシェルフのキーとして直接持つ場合は、シェルフのupdate方法を使用できます。

myShelvedDict.update(myDict)

インターフェイスは、shelveインターフェイスと大きく重複していdictます。

于 2011-07-10T10:48:36.163 に答える