私は著者の辞書を持っており、各著者は書籍の辞書であり、各書籍は単語のリストです。ネストされた辞書とマルチ処理
各プロセスが特定の著者からの特定の書籍を扱う、マルチプロセッシングのシナリオが必要です。
辞書とリストをインスタンス化するのにmanager.dict()
とmanager.list()
を使用しようとしましたが、辞書にまだデータが入力されていません。
これは、メイン辞書オブジェクトの宣言方法です。
import multiprocessing
from multiprocessing import Manager
manager = Manager()
allWords = manager.dict()
次にタスクdistribtion
def read_author(author):
global allWords
allWords[author] = manager.dict() # each author is a dictionary of books
jobs = []
for f in os.listdir(auth_dir):
p = multiprocessing.Process(target=read_doc, args=(author, auth_dir, f,))
jobs.append(p)
p.start()
return jobs
を行い、これが私のプロセスを作る機能である機能read_author
があります。
def read_doc(author_name, author_dir, doc_name):
global allWords
allWords[author_name][doc_name] = manager.list()
# document is loaded in the variable doc and it has a set of words
for word in doc.words:
allWords[author_name][doc_name].append(word)
ドキュメントプロジェクトグーテンベルクからTXTファイルであり、上記doc
オブジェクトがspacy
を使用して構築構文木です。
実際にread_doc
には、ドキュメントツリーの解析とバイグラムの抽出とカウントが含まれます。簡潔にするために、コードサンプルのこれらの部分はスキップしましたが、複数のCPUコアに分割したいというのはカウント作業なので、マルチプロセッシングを使用しています。
あなたのコードはどこですか? –
実行中の問題を示す[MCVE]を書き込んでみてください – pvg
@PedroLobito私はいくつかのコードを追加しました。もう一度見てください。 – Vahid