我的问题是:我需要通过不同的线程对一个文件执行频繁的读/写操作,我倾向于使用threading.RLock().acquire()和threading.RLock().release()来锁定资源(例如,一个txt文件)。问题是,您可以在某个线程中轻松地执行文件写入操作,如何在线程中获取返回值?这是我的示例代码:
FileLock = threading.RLock()
def FileReadingWriting(self, Input, Mode)
#### Input: things to write
#### Mode: 1 - write to the file
#### 2 - read from the file
FileLock.acquire()
if Mode == 1:
TheFile = open("example.txt", "w")
TheFile.write(Input)
TheFile.close()
elif Mode == 2:
TheFile = open("example.txt", "r")
Content = TheFile.read()
TheFile.close()
return Content #### Obviously this won't work, but if I want to get the content in a thread, how should I code?
FileLock.release()