python多线程飞速写入文件
程序员文章站
2022-07-07 19:59:36
乱序多线程写入举个最简单的例子,只要求快速写入即可,对顺序无要求时: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
上一篇: 妇科疾病最爱的“四少”人群,你中招了吗?
下一篇: 荐 数学建模与MATLAB-4