使用 Python 和 Windows 进行多处理
Multiprocessing with Python and Windows
我在 python 中有一个与 Thread 一起工作的代码,但我想切换到 Process,就好像我已经很好地理解了这会给我一个加速。
这里有线程的代码:
threads.append(Thread(target=getId, args=(my_queue, read)))
threads.append(Thread(target=getLatitude, args=(my_queue, read)))
代码可以将 return 放入队列中,在加入线程列表后,我可以检索结果。
更改代码和导入语句我现在的代码是这样的:
threads.append(Process(target=getId, args=(my_queue, read)))
threads.append(Process(target=getLatitude, args=(my_queue, read)))
但是它不执行任何操作并且队列是空的,线程队列不为空所以我认为它与Process有关。
我读过一些答案,其中进程 class 对 Windows 不起作用,这是真的吗,或者有办法让它起作用(添加 freeze_support() 没有帮助)?
在否定的情况下,windows 上的多线程实际上是在不同的内核上并行执行的?
参考:
Python multiprocessing example not working
Python code with multiprocessing does not work on Windows
(其中描述了 Windows 上不存在 fork)
编辑:
添加一些细节:
Process 的代码实际上在 centOS 上运行。
编辑2:
添加我的代码的简化版本和流程,代码在 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)
根据文档,您需要在函数定义之后添加以下内容。当 Python 创建子流程时,它们会导入您的脚本,因此 运行 在全局级别的代码将 运行 多次。对于您只想在主线程中 运行 的代码:
if __name__ == '__main__':
n = pd.read_csv("my.csv", sep =',', header = None).shape[0]
# etc.
在此 if
下缩进其余代码。
我在 python 中有一个与 Thread 一起工作的代码,但我想切换到 Process,就好像我已经很好地理解了这会给我一个加速。 这里有线程的代码:
threads.append(Thread(target=getId, args=(my_queue, read)))
threads.append(Thread(target=getLatitude, args=(my_queue, read)))
代码可以将 return 放入队列中,在加入线程列表后,我可以检索结果。 更改代码和导入语句我现在的代码是这样的:
threads.append(Process(target=getId, args=(my_queue, read)))
threads.append(Process(target=getLatitude, args=(my_queue, read)))
但是它不执行任何操作并且队列是空的,线程队列不为空所以我认为它与Process有关。 我读过一些答案,其中进程 class 对 Windows 不起作用,这是真的吗,或者有办法让它起作用(添加 freeze_support() 没有帮助)? 在否定的情况下,windows 上的多线程实际上是在不同的内核上并行执行的?
参考:
Python multiprocessing example not working
Python code with multiprocessing does not work on Windows
编辑: 添加一些细节: Process 的代码实际上在 centOS 上运行。
编辑2: 添加我的代码的简化版本和流程,代码在 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)
根据文档,您需要在函数定义之后添加以下内容。当 Python 创建子流程时,它们会导入您的脚本,因此 运行 在全局级别的代码将 运行 多次。对于您只想在主线程中 运行 的代码:
if __name__ == '__main__':
n = pd.read_csv("my.csv", sep =',', header = None).shape[0]
# etc.
在此 if
下缩进其余代码。