1
로드하려는 파일 수가 많아서 처리를 수행 한 다음 처리 된 데이터를 저장합니다.joblib를 사용하면 스크립트가 실행될 때 python이 점점 더 많은 RAM을 소비하게됩니다.
이from os import listdir
from os.path import dirname, abspath, isfile, join
import pandas as pd
import sys
import time
# Multi-threading
from joblib import Parallel, delayed
import multiprocessing
# Number of cores
TOTAL_NUM_CORES = multiprocessing.cpu_count()
# Path of this script's file
FILES_PATH = dirname(abspath(__file__))
def read_and_convert(f,num_files):
# Read the file
dataframe = pd.read_csv(FILES_PATH + '\\Tick\\' + f, low_memory=False, header=None, names=['Symbol', 'Date_Time', 'Bid', 'Ask'], index_col=1, parse_dates=True)
# Resample the data to have minute-to-minute data, Open-High-Low-Close format.
data_bid = dataframe['Bid'].resample('60S').ohlc()
data_ask = dataframe['Ask'].resample('60S').ohlc()
# Concatenate the OLHC data
data_ask_bid = pd.concat([data_bid, data_ask], axis=1, keys=['Bid', 'Ask'])
# Keep only non-weekend data (from Monday 00:00 until Friday 22:00)
data_ask_bid = data_ask_bid[(((data_ask_bid.index.weekday >= 0) & (data_ask_bid.index.weekday <= 3)) | ((data_ask_bid.index.weekday == 4) & (data_ask_bid.index.hour < 22)))]
# Save the processed and concatenated data of each month in a different folder
data_ask_bid.to_csv(FILES_PATH + '\\OHLC\\' + f)
print(f)
def main():
start_time = time.time()
# Get the paths for all the tick data files
files_names = [f for f in listdir(FILES_PATH + '\\Tick\\') if isfile(join(FILES_PATH + '\\Tick\\', f))]
num_cores = int(TOTAL_NUM_CORES/2)
print('Converting Tick data to OHLC...')
print('Using ' + str(num_cores) + ' cores.')
# Open and convert files in parallel
Parallel(n_jobs=num_cores)(delayed(read_and_convert)(f,len(files_names)) for f in files_names)
# for f in files_names: read_and_convert(f,len(files_names)) # non-parallel
print("\nTook %s seconds." % (time.time() - start_time))
if __name__ == "__main__":
main()
파일의 처음 몇이 정말 빨리 이런 식으로 처리되지만 속도는 스크립트가 더 처리하고 더 파일로 실수 받고 시작합니다이를 위해 나는 다음과 같은 코드가 있습니다. 더 많은 파일이 처리 될수록 RAM은 점진적으로 더 완벽 해집니다. Joblib은 파일을 순환하면서 uneeded 데이터를 플러시하지 않습니까? 병렬에서 실행중인 함수의 마지막 줄에 gc.collect()
추가