Optimize Python Code for Processing Large Historical Betting Data from bz2 Files in a tar Archive

I am working on a project where I need to process a large amount of Betfair historical betting data provided in a tar file. This tar file contains multiple bz2 files, each of which includes a single file with a JSON object per line.

The current approach involves extracting data from these bz2 files, parsing the JSON, and then loading the data into Pandas dataframes for further analysis. I’ve utilized concurrent.futures for parallel processing and tried to optimize file reading and data handling as much as possible. However, I’m looking for ways to further optimize this process, particularly in terms of reducing the overall processing time and improving memory usage.

Here is the relevant part of the code:

import tarfile
import bz2
import json
import pandas as pd
import io
from concurrent.futures import ProcessPoolExecutor, as_completed
from tqdm import tqdm
import time

def process_bz2_data(bundle):
    market_definition_list = []
    normal_entry_list = []
    for data in bundle:
        with bz2.open(io.BytesIO(data), 'rt', encoding='utf-8') as f:
            for line in f:
                try:
                    json_data = json.loads(line)
                    pt_time = json_data['pt']
                    mc_data = json_data['mc']
                    for info in mc_data:
                        if 'marketDefinition' in info:
                            market_definition_list.append({
                                'market_id': info['id'],
                                'start_datetime': info['marketDefinition']['openDate'],
                                **{f'runner_{i}_id': v['id'] for i, v in enumerate(info['marketDefinition']['runners'])},
                                **{f'runner_{i}_name': v['name'] for i, v in enumerate(info['marketDefinition']['runners'])},
                            })
                        else:
                            market_id = info['id']
                            for change in info['rc']:
                                last_traded_price = change.get('ltp', None)
                                runner_id = change['id']
                                normal_entry_list.append({
                                    'time': pt_time,
                                    'market_id': market_id,
                                    'runner_id': runner_id,
                                    'last_traded_price': last_traded_price
                                })
                except json.JSONDecodeError as e:
                    print(f"Error decoding JSON: {e}")
                except UnicodeDecodeError as e:
                    print(f"Error decoding Unicode: {e}")

    return market_definition_list, normal_entry_list

def load_json_to_dataframes(tar_file_path, batch_size=10):
    results = []
    with tarfile.open(tar_file_path, 'r') as tar:
        initial = time.perf_counter()
        files_to_process = [tar.extractfile(member).read() for member in tar.getmembers() if member.isfile() and member.name.endswith('.bz2')] # 41 seconds
        print(f"Elapsed time (Reading Files): {time.perf_counter() - initial:.2f} seconds")
        print(f"We have {len(files_to_process)} files to process.") # 183598

        batches = [files_to_process[i:i + batch_size] for i in range(0, len(files_to_process), batch_size)]

        with ProcessPoolExecutor() as executor:
            futures = [executor.submit(process_bz2_data, batch) for batch in batches]

            for future in tqdm(as_completed(futures), total=len(futures), desc="Processing files"): # 3 mins 51 seconds
                result = future.result()
                if result:
                    results.extend(result)

    initial = time.perf_counter()
    market_definitions = [item for result in results[0] for item in result]
    normal_entries = [item for result in results[1] for item in result]
    print(f"Elapsed time (Splitting Data): {time.perf_counter() - initial:.2f} seconds") # 0.00 seconds (does not seem right)

    initial = time.perf_counter()
    market_definition_df = pd.DataFrame(market_definitions)
    normal_entry_df = pd.DataFrame(normal_entries)
    print(f"Elapsed time (Creating DataFrames): {time.perf_counter() - initial:.2f} seconds") # 0.06 secodns (does not seem right)
    # Takes a couple of seconds before displaying the sucess message, not sure why.
    return market_definition_df, normal_entry_df

if __name__ == '__main__':
    tar_file_path = "./data (6).tar"
    market_definition_df, normal_entry_df = load_json_to_dataframes(tar_file_path)
    print('DataFrames loaded successfully.')

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