def example2(): # We now do not want to use /dev/shm or /run/shm and no ramfs paths # If /run/user/{uid} is available, we prefer it to /tmp # And we want to try /var/run as a last resort # If all fails, fallback to platform's tmp dir from memory_tempfile import MemoryTempfile # By the way, all paths with string {uid} will have it replaced with the user id tempfile = MemoryTempfile(preferred_paths=['/run/user/{uid}'], remove_paths=['/dev/shm', '/run/shm'], additional_paths=['/var/run'], filesystem_types=['tmpfs'], fallback=True) if tempfile.found_mem_tempdir(): print('We could use any of the followig paths: {}'.format(tempfile.get_usable_mem_tempdir_paths())) print('And we are using now: {}'.format(tempfile.gettempdir())) with tempfile.NamedTemporaryFile() as ntf: # use it as usual... pass
import numpy as np import pandas as pd import scipy.stats as st # import scipy as sp from PIL import Image, ImageDraw, ImageFont import matplotlib.pyplot as plt from memory_tempfile import MemoryTempfile tempfile = MemoryTempfile() PATH_ROOT_DIR = os.path.dirname(os.path.abspath(__file__)).replace("\\", "/") + "/" HOME_DIR = os.path.expanduser("~") TEMP_DIR = tempfile.gettempdir() + "/" def func_timer(f, args): start_time = time.time() r = f(*args) end_time = time.time() return end_time - start_time, r if __name__ == '__main__': # create random arr nums with a dimension m and amount n dimension = 10 n = 100000 assert 256**dimension >= n
import dill import gzip import os import sys # import tempfile from memory_tempfile import MemoryTempfile tempfile = MemoryTempfile() TEMP_ROOT_DIR_PATH = tempfile.gettempdir() TEMP_FOLDER_PATH = os.path.join(TEMP_ROOT_DIR_PATH, 'python_objs/') if not os.path.exists(TEMP_FOLDER_PATH): os.makedirs(TEMP_FOLDER_PATH) def save_object(obj_name, obj): FILE_PATH_ABS = TEMP_FOLDER_PATH + '{}.pkl.gz'.format(obj_name) with gzip.open(FILE_PATH_ABS, 'wb') as f: dill.dump(obj, f) def load_object(obj_name): FILE_PATH_ABS = TEMP_FOLDER_PATH + '{}.pkl.gz'.format(obj_name) with gzip.open(FILE_PATH_ABS, 'rb') as f: obj = dill.load(f) return obj def do_object_exist(obj_name): FILE_PATH_ABS = TEMP_FOLDER_PATH + '{}.pkl.gz'.format(obj_name) return os.path.exists(FILE_PATH_ABS)
# We now do not want to use /dev/shm or /run/shm and no ramfs paths # If /run/user/{uid} is available, we prefer it to /tmp # And we want to try /var/run as a last resort # If all fails, fallback to platform's tmp dir from memory_tempfile import MemoryTempfile import memory_tempfile # By the way, all paths with string {uid} will have it replaced with the user id tempfile = MemoryTempfile(fallback=False) if tempfile.found_mem_tempdir(): print('We could use any of the followig paths: {}'.format( tempfile.get_usable_mem_tempdir_paths())) print('And we are using now: {}'.format(tempfile.gettempdir())) with tempfile.NamedTemporaryFile() as ntf: # use it as usual... pass