add threading test for file write

This commit is contained in:
qiurui 2024-01-18 17:36:48 +08:00
parent 0004959614
commit 9ca6b259ba
1 changed files with 13 additions and 0 deletions

13
test/test_threading.py Normal file
View File

@ -0,0 +1,13 @@
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()