python多线程飞速写入文件

乱序多线程写入

举个最简单的例子,只要求快速写入即可,对顺序无要求时:

import threading

def write_string(string, path="test.csv"):
    with open(path, 'a') as f:
        f.write(string + "\r\n")

# 创建新线程
for i in range(15):
	# 这里每次循环都开一个线程,并写入"写入:" + i,args里指定参数,注意要使用list[]格式
    thread1 = threading.Thread(target=write_string, args=["写入: " + str(i)])
    thread1.start()

或是使用:

import threading

class WriteThread(threading.Thread):
    def __init__(self, string, file_path):
        threading.Thread.__init__(self)
        self.string = string
        self.file_path = file_path

    def run(self):
        write_string(self.file_path, self.string)


def write_string(file_path, string):
    with open(file_path, 'a') as f:
        f.write(string + "\r\n")

for i in range(15):
    thread1 = WriteThread("写入:" + str(i), "test.csv")
    thread1.start()

正序多线程写入

只需要改动三行:

import threading

threadLock = threading.Lock()

def write_string(string, path="test.csv"):
    threadLock.acquire() # 加个同步锁就好了
    with open(path, 'a') as f:
        f.write(string + "\r\n")
    threadLock.release()

# 创建新线程
for i in range(15):
    thread1 = threading.Thread(target=write_string, args=["写入: " + str(i)]).run()

你可能感兴趣的:(python,python,多线程)