@Rhys, sorry, but I don't understand the 'stacking' .... so, here's a slightly modified version of your script, demonstrating the write to the files happen in thread order despite what the order of the print(....In/Out) messages may appear on the terminal. If this does not clarify for you then i can't help any more without a clearer description of the issue.
cat test.py
import concurrent.futures
import random
import datetime
# Analysis of text packet
def Threads1(curr_section, index1):
words = open('test.txt', 'r', encoding='utf-8', errors='ignore').read().replace('"', '').split()
longest_recorded = []
for ii1 in words:
test1 = random.randint(1, 1000)
if test1 > 900: break
else: longest_recorded.append(ii1)
perc = (index1 / max1) * 100
print(str(datetime.datetime.now().time()) + ' In: ' + str([index1, str(int(perc))+'%']), flush=True)
return [index1, longest_recorded]
# Split text into packets
max1 = 20; count_Done = 0; ranger = [None for ii in range(0,max1)]
print(str(int((count_Done / max1) * 100)) + '%')
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor:
working_threads = {executor.submit(Threads1, curr_section, index1): curr_section for index1, curr_section in enumerate(ranger)}
for future in concurrent.futures.as_completed(working_threads):
count_Done += 1
current_result = future.result()
# Write to disk (random)
text1 = 'a: thread:' + str(current_result[0]) + ' : ' + str(datetime.datetime.now().time()) # + ':' + 'a' * (random.randint(1000, 1500) - 500)
with open('temp_Publish.txt', 'a', encoding='utf-8') as file: # append
file.write(text1 + '\n')
# Write to disk (random)
text2 = 'b :thread:' + str(current_result[0]) + ' : ' + str(datetime.datetime.now().time()) # + ':' + 'a' * (random.randint(1000, 1500) - 500)
with open('threads.txt', 'a', encoding='utf-8') as file: # append
file.write(text2 + '\n' )
print(str(datetime.datetime.now().time()) + ' Out: ' + str([current_result[0], str(int((count_Done / max1) * 100)) + '%']), flush=True)
#
# clear down any existing outputs
#
rm -f threads.txt temp_Publish.txt
#
#
python test.py
0%
22:55:08.067920 In: [0, '0%']
22:55:08.069660 In: [1, '5%']
22:55:08.069706 Out: [0, '5%']
22:55:08.071114 In: [2, '10%']
22:55:08.072442 In: [3, '15%']
22:55:08.072884 Out: [1, '10%']
22:55:08.073888 In: [4, '20%']
22:55:08.075159 In: [5, '25%']
22:55:08.076440 In: [6, '30%']
22:55:08.076826 Out: [2, '15%']
22:55:08.077735 In: [7, '35%']
22:55:08.079142 In: [8, '40%']
22:55:08.079579 Out: [3, '20%']
22:55:08.080298 In: [9, '45%']
22:55:08.081270 In: [10, '50%']
22:55:08.081626 Out: [4, '25%']
22:55:08.082362 In: [11, '55%']
22:55:08.083296 In: [12, '60%']
22:55:08.084155 In: [13, '65%']
22:55:08.084434 Out: [5, '30%']
22:55:08.085052 In: [14, '70%']
22:55:08.086036 In: [15, '75%']
22:55:08.086921 In: [16, '80%']
22:55:08.088107 In: [17, '85%']
22:55:08.089462 In: [18, '90%']
22:55:08.089851 Out: [6, '35%']
22:55:08.090772 In: [19, '95%']
22:55:08.091278 Out: [7, '40%']
22:55:08.091479 Out: [8, '45%']
22:55:08.091696 Out: [9, '50%']
22:55:08.091905 Out: [10, '55%']
22:55:08.092107 Out: [11, '60%']
22:55:08.092311 Out: [12, '65%']
22:55:08.092508 Out: [13, '70%']
22:55:08.092703 Out: [14, '75%']
22:55:08.092857 Out: [15, '80%']
22:55:08.093000 Out: [16, '85%']
22:55:08.093144 Out: [17, '90%']
22:55:08.093291 Out: [18, '95%']
22:55:08.093461 Out: [19, '100%']
#
# show file contents - side by side for convenience
#
paste threads.txt temp_Publish.txt
b :thread:0 : 22:55:08.068613 a: thread:0 : 22:55:08.068403
b :thread:1 : 22:55:08.071506 a: thread:1 : 22:55:08.070138
b :thread:2 : 22:55:08.074275 a: thread:2 : 22:55:08.072966
b :thread:3 : 22:55:08.078270 a: thread:3 : 22:55:08.077831
b :thread:4 : 22:55:08.080626 a: thread:4 : 22:55:08.079636
b :thread:5 : 22:55:08.081744 a: thread:5 : 22:55:08.081663
b :thread:6 : 22:55:08.087198 a: thread:6 : 22:55:08.085128
b :thread:7 : 22:55:08.091212 a: thread:7 : 22:55:08.089909
b :thread:8 : 22:55:08.091389 a: thread:8 : 22:55:08.091321
b :thread:9 : 22:55:08.091606 a: thread:9 : 22:55:08.091536
b :thread:10 : 22:55:08.091819 a: thread:10 : 22:55:08.091743
b :thread:11 : 22:55:08.092018 a: thread:11 : 22:55:08.091949
b :thread:12 : 22:55:08.092238 a: thread:12 : 22:55:08.092154
b :thread:13 : 22:55:08.092434 a: thread:13 : 22:55:08.092353
b :thread:14 : 22:55:08.092616 a: thread:14 : 22:55:08.092552
b :thread:15 : 22:55:08.092811 a: thread:15 : 22:55:08.092747
b :thread:16 : 22:55:08.092954 a: thread:16 : 22:55:08.092890
b :thread:17 : 22:55:08.093098 a: thread:17 : 22:55:08.093034
b :thread:18 : 22:55:08.093244 a: thread:18 : 22:55:08.093178
b :thread:19 : 22:55:08.093413 a: thread:19 : 22:55:08.093326