Don't read the whole file at once. Reading all lines with file.readlines() uses too much memory for big files.
Read the file line by line. Use a loop to read each line.
Assign a line number to each line. This helps you keep track of the order.
Use ProcessPoolExecutor instead of ThreadPoolExecutor. For CPU-heavy tasks, processes are faster because of Python's GIL.
Submit tasks with their line numbers. When you process a line also pass its line number.
Collect the results in a way that keeps order. You can store results in a list or dictionary using the line numbers as keys.
Write the results in order. After all lines are processed, write them to the output file in the original order.