I want to add concurrency.futures
module asynchronous I/O reading to my script. I want file to be read one time, then the result to be worked on.
As logic of module does not align with it I created two different functions which separately reads two separate time the file, as pandas dataframe and then gives me the result.
import pandas as pd
import sys,os, time,re
import concurrent.futures
start=(time.perf_counter())
def getting_file_path(fileName):
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
path_actual = os.getcwd()
path_main_folder = path_actual[:-4]
path_result = path_main_folder + fileName
print('frozen path',os.path.normpath(path_result))
return path_result
else:
return fileName
def read_keys_dropdown():
global lst_dropdown_keys
file_to_read = pd.read_json(getting_file_path('./ConfigurationFile/configFile.csv'))
lst_dropdown_keys=list(file_to_read.to_dict().keys())
lst_dropdown_keys.pop(0)
lst_dropdown_keys.pop(-1)
return lst_dropdown_keys
def read_url():
pattern = re.compile(r"^(?:/.|[^//])*/((?:\\.|[^/\\])*)/")
file_to_read=pd.read_json(getting_file_path('./ConfigurationFile/configFile.csv'))
result = (re.match(pattern, file_to_read.values[0][0]))
return pattern.match(file_to_read.values[0][0]).group(1)
with concurrent.futures.ThreadPoolExecutor() as executor:
res_1=executor.submit(read_keys_dropdown)
res_2=executor.submit(read_url)
finish=(time.perf_counter())
print(res_1.result(),res_2.result(),finish-start,sep=';')
Before, I was doing it differently. I was reading file_to_read = pd.read_json(getting_file_path('./ConfigurationFile/configFile.csv'))
in global scope then using that variable name in both functions.
I tried to do something like reading data, and then working on result it gave me Futures
object has no attribute
to_dict, nor values[0]...so, if I need to fasten my script and concurrency or threading modules are better choice for I/O reading files, then how else I can use them in my script?