How do I reduce the time to read parquet files with pandas and futures

Python version: 3.11.8

Goal: Read 1.000 parquet files from Amazon bucket faster.

Approach: I am using the libraries boto3 and pandas to read the parquet files. I could retrieve the files sequentially. However, I want to reduce the time reading the parquet files by using a parallel execution with the library concurrent.futures. For that reason, I decided to implement the ProcessPoolExecutor, but I got stuck because I do not know how to create my project to implement it. This is my current attempt:

I have two scripts. The first script will read the parquet files and the second script will transform or manipulate all the received data.

script1.py

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>from boto3 import Session
from pandas import DataFrame,concat, read_parquet
from concurrent.futures import ProcessPoolExecutor
from concurrent.futures import wait
def retrieve_s3_bucket(bucket_name: str,
access_key_id: str,
secret_access_key: str,
prefix: str):
### some code to retrieve objects from S3
return objects
def read_parquet_file(object) -> DataFrame:
buffer = BytesIO()
object.Object().download_fileobj(buffer)
df_object = read_parquet(buffer)
return df_object
def get_data_from_bucket(objects,
columns_to_delete: list,
columns_to_format: list,
restaurant_ids: list) -> DataFrame:
list_dfs = []
with ProcessPoolExecutor(2) as exe:
# submit tasks and collect futures
for object in objects:
df_object = exe.submit(read_parquet_file(object),object)
if df_object.empty:
print("Empty")
else:
list_dfs.append(df_object)
# wait for all tasks to complete
print('Waiting for tasks to complete...')
wait(list_dfs)
data = concat(list_dfs,ignore_index=True)
return data
# protect the entry point
if __name__ == '__main__':
# start the process pool
get_data_from_bucket()
</code>
<code>from boto3 import Session from pandas import DataFrame,concat, read_parquet from concurrent.futures import ProcessPoolExecutor from concurrent.futures import wait def retrieve_s3_bucket(bucket_name: str, access_key_id: str, secret_access_key: str, prefix: str): ### some code to retrieve objects from S3 return objects def read_parquet_file(object) -> DataFrame: buffer = BytesIO() object.Object().download_fileobj(buffer) df_object = read_parquet(buffer) return df_object def get_data_from_bucket(objects, columns_to_delete: list, columns_to_format: list, restaurant_ids: list) -> DataFrame: list_dfs = [] with ProcessPoolExecutor(2) as exe: # submit tasks and collect futures for object in objects: df_object = exe.submit(read_parquet_file(object),object) if df_object.empty: print("Empty") else: list_dfs.append(df_object) # wait for all tasks to complete print('Waiting for tasks to complete...') wait(list_dfs) data = concat(list_dfs,ignore_index=True) return data # protect the entry point if __name__ == '__main__': # start the process pool get_data_from_bucket() </code>
from boto3 import Session
from pandas import DataFrame,concat, read_parquet
from concurrent.futures import ProcessPoolExecutor
from concurrent.futures import wait

def retrieve_s3_bucket(bucket_name: str, 
                       access_key_id: str,
                       secret_access_key: str,
                       prefix: str):
 ### some code to retrieve objects from S3
return objects

def read_parquet_file(object) -> DataFrame:

    buffer = BytesIO()
    object.Object().download_fileobj(buffer)
    
    df_object = read_parquet(buffer) 
    return df_object

def get_data_from_bucket(objects,
                          columns_to_delete: list,
                          columns_to_format: list,
                          restaurant_ids: list) -> DataFrame:

  
  list_dfs = []

  with ProcessPoolExecutor(2) as exe:
    # submit tasks and collect futures
    for object in objects:
      df_object = exe.submit(read_parquet_file(object),object)
      if df_object.empty:
        print("Empty")
      else:  
        list_dfs.append(df_object)
    
    # wait for all tasks to complete
    print('Waiting for tasks to complete...')
    wait(list_dfs)
    
  data = concat(list_dfs,ignore_index=True)

  return data

# protect the entry point
if __name__ == '__main__':
  # start the process pool
  get_data_from_bucket()  

script2.py:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>from script1 import get_data_from_bucket, retrieve_s3_bucket
def main(bucket_name: str,
access_key_id: str,
secret_access_key: str,
prefix: str) -> str:
try:
data ={}
objects = retrieve_s3_bucket(bucket_name, access_key_id,secret_access_key,prefix)
df = get_data_from_bucket(objects,
columns_to_delete,
columns_to_format,
list_restaurants)
except Exception as e:
logging.error(str(e))
data['error_message'] = str(e)
data['stack_trace'] = print_exc()
finally:
return dumps(data)
</code>
<code>from script1 import get_data_from_bucket, retrieve_s3_bucket def main(bucket_name: str, access_key_id: str, secret_access_key: str, prefix: str) -> str: try: data ={} objects = retrieve_s3_bucket(bucket_name, access_key_id,secret_access_key,prefix) df = get_data_from_bucket(objects, columns_to_delete, columns_to_format, list_restaurants) except Exception as e: logging.error(str(e)) data['error_message'] = str(e) data['stack_trace'] = print_exc() finally: return dumps(data) </code>
from script1 import get_data_from_bucket, retrieve_s3_bucket

def main(bucket_name: str,
                    access_key_id: str, 
                    secret_access_key: str, 
                    prefix: str) -> str:
  
  try:
    
    data ={}

    objects = retrieve_s3_bucket(bucket_name, access_key_id,secret_access_key,prefix)
    
    df = get_data_from_bucket(objects,
                              columns_to_delete,
                              columns_to_format,
                              list_restaurants)
    
  except Exception as e:
        
    logging.error(str(e))
    data['error_message'] = str(e)
    data['stack_trace'] = print_exc()
    
  finally:
    return dumps(data)

I am getting the following error during execution:

An attempt has been made to start a new process before the current
process has finished its bootstrapping phase

How should I include the concurrent.futures in my project and to handle properly erros during the reading?. Forgive me if I am doing something wrong but I am pretty new with concept of parallelism.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật