Search code examples
pythonpython-multithreading

How to write individual txt files utilising multithreading


Hey all trust that you're well, I'm trying to get each thread to create separate .txt files and write to each txt file individually.

Example: process 1 opens pri0.txt and writes 10 names, whilst process two opens pri1.txt and write 10 names

Outcome I received: pri0.txt as well as pri1.txt were created, process 1 wrote 1 line to pri0.txt and the rest in pri1.txt and process 2 wrote to pri1.txt.

Below is the code

import time
import json
import threading
import itertools



limit = 10

def test(process):

    z = 0

    for _ in itertools.count(start=1):

        if z == limit:
            break

        z += 1

        time.sleep(1)

        with open(f'pri{i}.txt', 'a') as file:
            file.write(process[f'user{z}'] + '\n')

process = open('user.json',)
pro = json.load(process)
process.close()

process_count = 2

thread_list = []

for i in range(process_count):
    t = threading.Thread(name=f'Account {i}', target=test, args=[process[i]])
    t.start()
    time.sleep(1)
    print(t.name + ' started')
    thread_list.append(t)

for thread in thread_list:
    thread.join()

Solution

  • This code writes in two different files individually. As you can see in the output, the two threads are writing at the same time.

    import threading
    
    def writeToFile(fileNumber):
        with open(f'File{fileNumber}.txt', 'w') as file:
                for i in range(5):
                    print(f"wroteToFile: {fileNumber}")
                    file.write("hello\n")
    
    thread_list = []
    
    thread_list.append(threading.Thread(target=writeToFile, args=[0]))
    thread_list.append(threading.Thread(target=writeToFile, args=[1]))
    
    # start the threads
    for index, thread in enumerate(thread_list):
        thread.start()
        print(f"ThreadNumber {index} started")
    
    # wait until all threads are terminated
    for thread in thread_list:
        thread.join()