From 9ca6b259ba708ffa958a4a58851e7677eccb3f92 Mon Sep 17 00:00:00 2001 From: qiurui Date: Thu, 18 Jan 2024 17:36:48 +0800 Subject: [PATCH] add threading test for file write --- test/test_threading.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 test/test_threading.py diff --git a/test/test_threading.py b/test/test_threading.py new file mode 100644 index 0000000..32f9234 --- /dev/null +++ b/test/test_threading.py @@ -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() \ No newline at end of file