I upload 130k json files.
I do this with Python
:
import os
import json
import pandas as pd
path = "/my_path/"
filename_ending = '.json'
json_list = []
json_files = [file for file in os.listdir(f"{path}") if file.endswith(filename_ending)]
import time
start = time.time()
for jf in json_files:
with open(f"{path}/{jf}", 'r') as f:
json_data = json.load(f)
json_list.append(json_data)
end = time.time()
and it takes 60 seconds.
I do this with multiprocessing
:
import os
import json
import pandas as pd
from multiprocessing import Pool
import time
path = "/my_path/"
filename_ending = '.json'
json_files = [file for file in os.listdir(f"{path}") if file.endswith(filename_ending)]
def read_data(name):
with open(f"/my_path/{name}", 'r') as f:
json_data = json.load(f)
return json_data
if __name__ == '__main__':
start = time.time()
pool = Pool(processes=os.cpu_count())
x = pool.map(read_data, json_files)
end = time.time()
and it takes 53 seconds.
I do this with ray
:
import os
import json
import pandas as pd
from multiprocessing import Pool
import time
import ray
path = "/my_path/"
filename_ending = '.json'
json_files = [file for file in os.listdir(f"{path}") if file.endswith(filename_ending)]
start = time.time()
ray.shutdown()
ray.init(num_cpus=os.cpu_count()-1)
@ray.remote
def read_data(name):
with open(f"/my_path/{name}", 'r') as f:
json_data = json.load(f)
return json_data
all_data = []
for jf in json_files:
all_data.append(read_data.remote(jf))
final = ray.get(all_data)
end = time.time()
and it takes 146 seconds.
My question is why ray
takes so much time?
Is it because:
1) ray is relatively slow for relatively small amount of data?
2) I am doing something wrong in my code?
3) ray
is not that useful?