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