awsEFS挂载时使用numpymemmap随机访问非常缓慢。
创始人
2024-09-25 06:02:29
0

这可能是因为aws EFS挂载的数据网络带宽限制了读写速度。为了解决这个问题,我们可以通过缓存数据到本地磁盘上来加速读写操作。以下是一个使用numpy memmap缓存数据到本地磁盘的示例代码:

import numpy as np
import os

# Specify the file path and shape of the numpy array
file_path = '/mnt/efs/data.npy'
array_shape = (100000, 10000)

# If the file does not exist, create it and fill it with random data
if not os.path.exists(file_path):
    data = np.random.rand(*array_shape)
    np.save(file_path, data)
else:
    data = np.load(file_path, mmap_mode='r')

# Create a local cache of the memmap object
cache_dir = '/tmp/data_cache'
if not os.path.exists(cache_dir):
    os.mkdir(cache_dir)
cache_file = os.path.join(cache_dir, os.path.basename(file_path))
if not os.path.exists(cache_file):
    cache = np.memmap(cache_file, mode='w+', shape=array_shape)
    cache[:] = data[:]
else:
    cache = np.memmap(cache_file, mode='r', shape=array_shape)

# Read random data from the array
indices = np.random.randint(0, array_shape[0], size=1000)
for idx in indices:
    row = cache[idx, :]

在这个示例中,我们首先指定了numpy数组的形状和存储路径。如果文件不存在,我们将随机数据填充到numpy数组中并保存到指定路径。如果文件已存在,则从该文件中加载数据并使用mmap_mode参数创建一个numpy memmap对象。接下来,我们将本地磁盘上的缓存文件所在目录设置为/tmp/data_cache,如果目录不存在则创建该目录,并将memmap对象的数据复制到本地磁盘上的缓存文件中。每次访问文件时

相关内容

热门资讯

实测揭晓!云扑克辅助挂,微扑克... 实测揭晓!云扑克辅助挂,微扑克wpk透视辅助,技巧教程(本然是有挂)(1)实测揭晓!云扑克辅助挂,微...
教程辅助!微扑克发牌机制,wp... 教程辅助!微扑克发牌机制,wpk透明挂,详细教程(固有真的是有挂)是一款可以让一直输的玩家,快速成为...
一分钟揭秘!红龙扑克辅助器,w... 一分钟揭秘!红龙扑克辅助器,wepoke辅助挂,靠谱教程(好像有挂)1、构建自己的微扑克辅助插件;2...
今日头条!wpk透视辅助合作,... 今日头条!wpk透视辅助合作,wepoke有辅助挂,德州论坛(果然是真的有挂)1、许多玩家不知道we...
实测交流!云扑克德州PK,aa... 实测交流!云扑克德州PK,aapoker有猫腻,安装教程(本然存在有挂);无聊就玩这款aapoker...
发现玩家!菠萝德州有挂吗,德州... 发现玩家!菠萝德州有挂吗,德州AI智能辅助机器人,AA德州教程(竟然是有挂)1、构建自己的微扑克辅助...
最新科技!鱼扑克fishpok... 最新科技!鱼扑克fishpoker俱乐部辅助,wepoke确实有挂,2025新版(一直存在有挂),您...
最新技巧!云扑克辅助器怎么卖,... 最新技巧!云扑克辅助器怎么卖,wpk辅助挂,介绍教程(固有有挂)是一款可以让一直输的玩家,快速成为一...
一分钟揭秘!wpk德州扑克,智... 一分钟揭秘!wpk德州扑克,智星德州菠萝有挂吗,安装教程(一向是有挂)您好,智星德州菠萝有挂吗,确实...
科技通报!wepoke真的有挂... 科技通报!wepoke真的有挂吗,德扑之星辅助器购买,大神讲解(总是真的是有挂)是一款可以让一直输的...