私はPythonでThreadと連携するコードを持っていますが、私はスピードアップをうまく理解しているかのようにProcessに切り替える必要があります。 はここスレッドでコードがあります:PythonとWindowsを使ったマルチプロセッシング
threads.append(Thread(target=getId, args=(my_queue, read)))
threads.append(Thread(target=getLatitude, args=(my_queue, read)))
コードがキューにリターンを入れて動作し、スレッドリストに参加した後、私は結果を取得することができます。 は、コードと私のコードは、現在そのようなものですimport文を変更する:
threads.append(Process(target=getId, args=(my_queue, read)))
threads.append(Process(target=getLatitude, args=(my_queue, read)))
ので、私はそれがプロセスに関連していると思うことは何も実行されず、キューは、キューが空でないスレッドで、空であるしかし、 。 WindowsでProcessクラスが動作しないという回答を読んだり、それを動作させる方法があります(freeze_support()を追加しても効果がありません)。 否定的なケースでは、ウィンドウ上のマルチスレッドは実際には異なるコア上で並列に実行されますか?
REF: いくつかの詳細を追加する:
編集(フォークは、Windows上に存在しないことが記載されている)
Python multiprocessing example not working
Python code with multiprocessing does not work on Windows
Multiprocessing process does not join when putting complex dictionary in return queue コードProcessでは実際にはcentOS上で作業しています。
EDIT2: プロセスと私のコードの簡易版を追加し、CentOSの
[ドキュメント]パーimport pandas as pd
from multiprocessing import Process, freeze_support
from multiprocessing import Queue
#%% Global variables
datasets = []
latitude = []
def fun(key, job):
global latitude
if(key == 'LAT'):
latitude.append(job)
def getLatitude(out_queue, skip = None):
latDict = {'LAT' : latitude}
out_queue.put(latDict)
n = pd.read_csv("my.csv", sep =',', header = None).shape[0]
print("Number of baboon:" + str(n))
read = []
for i in range(0,n):
threads = []
my_queue = Queue()
threads.append(Process(target=getLatitude, args=(my_queue, read)))
for t in threads:
freeze_support() # try both with and without this line
t.start()
for t in threads:
t.join()
while not my_queue.empty():
try:
job = my_queue.get()
key = list(job.keys())
fun(key[0],job[key[0]])
except:
print("END")
read.append(i)
プロセスはWindows上で動作しますが、問題を再現する完全なコード例が表示されます。ガイドラインについては、[mcve]を参照してください。 –
質問にコードを追加しました。 @マークトーロネン –