欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python多线程飞速写入文件

程序员文章站 2022-03-30 10:16:40
乱序多线程写入举个最简单的例子,只要求快速写入即可,对顺序无要求时:import threadingdef 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 =...

乱序多线程写入

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

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()

本文地址:https://blog.csdn.net/weixin_35757704/article/details/107175302