私はここでマルチプロセッシングチュートリアルを行っていました:http://pymotw.com/2/multiprocessing/basics.html
以下のスクリプトを演習として作成しました。スクリプトは機能しているようで、taskmgrで5つの新しいPythonプロセスが実行されています。しかし、私のprintステートメントは、同じフォルダーが複数回検索されていることを出力します。
異なるプロセス間で作業を分割するのではなく、各プロセスに全体の作業負荷を与えているのではないかと疑っています。私は何か間違ったことをしていて非効率的だと確信しています。誰かが私のエラーを指摘してもらえますか?
私がこれまでに持っているもの:
def email_finder(msg_id):
for folder in os.listdir(sample_path):
print "Searching through folder: ", folder
folder_path = sample_path + '\\' + folder
for file in os.listdir(os.listdir(folder_path)):
if file.endswith('.eml'):
file_path = folder_path + '\\' + file
email_obj = email.message_from_file(open(file_path))
if msg_id in email_obj.as_string().lower()
shutil.copy(file_path, tmp_path + '\\' + file)
return 'Found: ', file_path
else:
return 'Not Found!'
def worker():
msg_ids = cur.execute("select msg_id from my_table").fetchall()
for i in msg_ids:
msg_id = i[0].encode('ascii')
if msg_id != '':
email_finder(msg_id)
return
if __name__ == '__main__':
jobs = []
for i in range(5):
p = multiprocessing.Process(target=worker)
jobs.append(p)
p.start()