add threading test for file write
This commit is contained in:
parent
0004959614
commit
9ca6b259ba
|
@ -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()
|
Loading…
Reference in New Issue