2
これは私の問題です:私は頻繁に別のスレッドを介して1つのファイルに読み取り/書き込み操作を実行する必要があり、私はthreading.RLock()を使用する傾向があるacquire()とthreading.RLock()。release() (例えば、1つのtxtファイル)。問題は、特定のスレッドでファイル書き込み操作を簡単に行うことができることです。どのようにスレッドの戻り値を取得しますか?私のサンプルコードは次のとおりです。Pythonで頻繁にファイルの読み書き操作(マルチスレッド)を実行するにはどうすればよいですか?
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()