[英]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)))
该代码将返回值放入队列中,并在线程列表上加入后,我可以检索结果。 更改代码和导入语句我的代码现在是这样的:
threads.append(Process(target=getId, args=(my_queue, read)))
threads.append(Process(target=getLatitude, args=(my_queue, read)))
但是它不执行任何操作并且队列为空,线程队列不为空所以我认为它与进程有关。 我读过 Process 类在 Windows 上不起作用的答案是真的,还是有办法让它工作(添加 freeze_support() 没有帮助)? 在消极的情况下,windows 上的多线程实际上是在不同的内核上并行执行的?
参考:
具有多处理功能的 Python 代码在 Windows 上不起作用
将复杂字典放入返回队列时,多处理进程未加入(其中描述了 Windows 上不存在 fork)
编辑:添加一些细节:带有 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)
根据文档,在函数定义之后需要以下内容。 当 Python 创建子进程时,它们会导入您的脚本,因此在全局级别运行的代码将多次运行。 对于只想在主线程中运行的代码:
if __name__ == '__main__':
n = pd.read_csv("my.csv", sep =',', header = None).shape[0]
# etc.
缩进此if
下的其余代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.