python - Python - 多处理卡住
问题描述
我正在尝试对 MNIST 数据集并行执行某些操作。使用多处理,它会卡住并且永远不会结束。我尝试将 close 和 join 函数放在 for 循环内部或外部。我还尝试将池实例化放在函数中,似乎也不起作用。我在这里做错了什么?不知道我应该在哪里解决这个问题。非常感谢您提前提供的帮助
class MnistPartialDataset(Dataset):
def __init__(self, data, transform=None):
self.data = data
def __getitem__(self, index):
return self.data[index]
def __len__(self):
return len(self.data)
def create_tasks(train_pool, test_pool):
train_thread_result = []
start = time.time()
for k, v in train_tasks.items():
tc_data = train_pool.map_async(test_function, v)
train_thread_result.append(tc_data)
train_pool.close()
train_pool.join()
for i in range(len(train_thread_result)):
train_dataset[i] = MnistPartialDataset(train_thread_result[i].get())
end = time.time()
print("tc time: " + str(end-start))
test_thread_result = []
start = time.time()
for k, v in test_tasks.items():
tc_data = test_pool.map_async(test_function, v)
test_thread_result.append(tc_data)
test_pool.close()
test_pool.join()
for i in range(len(test_thread_result)):
test_dataset[i] = MnistPartialDataset(test_thread_result[i].get())
end = time.time()
def main():
train_pool = multiprocessing.Pool(processes = multiprocessing.cpu_count()-1)
test_pool = multiprocessing.Pool(processes = multiprocessing.cpu_count()-1)
create_tasks(train_pool, test_pool)
pool.close()
pool.terminate()
解决方案
推荐阅读
- android - 在android RecyclerView中与linearLayoutManager.setreverselayout一起使用时adapter.notifyItemInserted(position == 0)没有动画?
- algorithm - 如何使用 BFS 在未加权和无向图中的两个顶点之间找到 1 个以上的连续最短路径?
- cakephp - Cakephp 4 Windows 安装问题
- python - CNN 音频分类器训练了 3 个类,预测的总和应该小于 1
- r - image_append 变量的动态数量
- python - 对于二进制流,Python 是否具有等效于 io.TextIOWrapper() 的功能,例如压缩算法?
- r - 是否有一种方法可以将整数值分配给 R 中的因子
- wordpress-rest-api - react-admin 对 WordPress API 的支持?
- c - 如何从 linux 模块正确禁用 SMAP?
- numpy - 如何遍历最后一维的切片