The pit of using multithreading to process documents

One, the problem is found:

When using multiple threads to process documents, I found that when printing data to the console, the tasks are executed in parallel and executed at
the same time. However, when writing the same text to txt text, the content of the text is executed in a single thread.

1. Below is the code

#codint=utf-8

from multiprocessing import Process
import time

def run(Str,Num):
    m = 1
    with open("test.txt","a",encoding="utf-8") as FileObj:
        while True:
            FileObj.write("run(%s)方法正在运行,第 %d 次运行,n的值是 %d\n"%(Str,m,Num))
            print("run(%s)方法正在运行,第 %d 次运行,n的值是 %d\n"%(Str,m,Num))
            m += 1
            time.sleep(1)
            if m >= 10:
                break
if __name__ == '__main__':
    with open("test.txt", "a",encoding="utf-8") as FileObj:
        FileObj.write("主进程启动。。。\n")
        print("主进程启动。。。\n")
        # 创建子进程
        # target说明进程执行的任务
        n = 1
        PObj = Process(target=run,args=("nice",n))
        # 启动进程
        PObj.start()
        while True:
            FileObj.write("main()方法正在运行,第 %d 次运行\n"%n)
            print("main()方法正在运行,第 %d 次运行\n"%n)
            n += 1
            time.sleep(1)
            if n >= 10:
                break

2. Print screenshots on the console

Insert picture description here

3. txt text screenshot

Insert picture description here

Second, the cause of the problem

  1. This problem occurs because, after the print statement is executed, the printed data is output directly
  2. After the sentence to write the file is executed, the data is saved in the computer's memory. After the task is executed, the data in the memory is saved to the file, and the file is closed to save the data successfully.
  3. Therefore, the data saved in the text is not parallel

Three, the solution

  1. After the file write statement in each method is executed, a save operation is performed immediately, so which method is called will save the data immediately

Fourth, the solution effect

  1. After solving according to the above method, the data in the file is also parallel
    Insert picture description here

Guess you like

Origin blog.csdn.net/zhuan_long/article/details/112213977