python多线程和文件锁定问题
我用两种方法实现了多线程代码,但在这两种方法中都出现了错误。有人能解释一下问题的原因吗 在版本1中,我得到了一个异常,表示有两个参数传递给writekey函数,而不是一个。 在版本2中,其中一个线程读取空行,因此在处理空字符串时引发异常 我正在使用锁,它不应该阻止多个线程同时访问函数或文件吗 第1版:python多线程和文件锁定问题,python,multithreading,exception,locking,Python,Multithreading,Exception,Locking,我用两种方法实现了多线程代码,但在这两种方法中都出现了错误。有人能解释一下问题的原因吗 在版本1中,我得到了一个异常,表示有两个参数传递给writekey函数,而不是一个。 在版本2中,其中一个线程读取空行,因此在处理空字符串时引发异常 我正在使用锁,它不应该阻止多个线程同时访问函数或文件吗 第1版: class SomeThread(threading.Thread): def __init__(self, somequeue, lockfile): threading
class SomeThread(threading.Thread):
def __init__(self, somequeue, lockfile):
threading.Thread.__init__(self)
self.myqueue = somequeue
self.myfilelock = lockfile
def writekey(key):
if os.path.exists(os.path.join('.', outfile)):
with open(outfile, 'r') as fc:
readkey = int(fc.readline().rstrip())
os.remove(os.path.join('.', outfile))
with open(outfile, 'w') as fw:
if readkey > key:
fw.write(str(readkey))
else:
fw.write(str(key))
def run(self):
while(True):
dict = self.myqueue.get()
self.myfilelock.acquire()
try:
self.writekey(dict.get("key"))
finally:
self.myfilelock.release()
self.myqueue.task_done()
populateQueue() # populate queue with objects
filelock = threading.Lock()
for i in range(threadnum):
thread = SomeThread(somequeue, filelock)
thread.setDaemon(True)
thread.start()
somequeue.join()
def writekey(key):
if os.path.exists(os.path.join('.', outfile)):
with open(outfile, 'r') as fc:
# do something...
os.remove(os.path.join('.', outfile))
with open(outfile, 'w') as fw:
# do something...
class SomeThread(threading.Thread):
def __init__(self, somequeue, lockfile):
threading.Thread.__init__(self)
self.myqueue = somequeue
self.myfilelock = lockfile
def run(self):
while(True):
dict = self.myqueue.get()
self.myfilelock.acquire()
try:
writekey(dict.get("key"))
finally:
myfilelock.release()
self.myqueue.task_done()
# Same as above ....
第2版:
class SomeThread(threading.Thread):
def __init__(self, somequeue, lockfile):
threading.Thread.__init__(self)
self.myqueue = somequeue
self.myfilelock = lockfile
def writekey(key):
if os.path.exists(os.path.join('.', outfile)):
with open(outfile, 'r') as fc:
readkey = int(fc.readline().rstrip())
os.remove(os.path.join('.', outfile))
with open(outfile, 'w') as fw:
if readkey > key:
fw.write(str(readkey))
else:
fw.write(str(key))
def run(self):
while(True):
dict = self.myqueue.get()
self.myfilelock.acquire()
try:
self.writekey(dict.get("key"))
finally:
self.myfilelock.release()
self.myqueue.task_done()
populateQueue() # populate queue with objects
filelock = threading.Lock()
for i in range(threadnum):
thread = SomeThread(somequeue, filelock)
thread.setDaemon(True)
thread.start()
somequeue.join()
def writekey(key):
if os.path.exists(os.path.join('.', outfile)):
with open(outfile, 'r') as fc:
# do something...
os.remove(os.path.join('.', outfile))
with open(outfile, 'w') as fw:
# do something...
class SomeThread(threading.Thread):
def __init__(self, somequeue, lockfile):
threading.Thread.__init__(self)
self.myqueue = somequeue
self.myfilelock = lockfile
def run(self):
while(True):
dict = self.myqueue.get()
self.myfilelock.acquire()
try:
writekey(dict.get("key"))
finally:
myfilelock.release()
self.myqueue.task_done()
# Same as above ....
在版本1中,
def writekey(key)
应以“self”作为第一个参数声明,即
def writekey(self, key):
版本2中的问题不太清楚。我假设在读取outfile
时正在读取一个空行。这是正常现象,表示已到达文件末尾。通常情况下,你会打破你的阅读循环。通常,最好在for
循环中逐行读取文件,例如:
with open(outfile, 'r') as fc:
for line in fc:
# process the line
for循环将在到达文件末尾时自然终止。如果您可以发布异常,这将非常有用。线程线程2中的异常:回溯(最近一次调用):…在运行self.writekey(dict.get(“key”))文件中…,在writekey readkey=int(fc.readline().rstrip())ValueError:invalid literal for int()对于基数10:“”尝试了这种方式。版本2也出现了同样的错误。我在writekey()函数中读取文件并将其转换为整数(int(fc.readline().rstrip())。异常为:线程thread-4中的异常,ValueError:以10为基数的int()的文本无效:“”由于行读取为空,引发异常。但是,它应该在writekey()函数中写入。实际上,outfile只有一行存储密钥。打开输出文件后,将读取上一个密钥,然后删除该文件。作为参数传递的键随后写入新创建的输出文件。
int(“”)
引发该异常。不要处理空行。空行表示文件结束-没有可供您处理的数据。您没有显示该代码,因此我们无法判断发生了什么。此外,无需删除文件-open(outfile,'w')
将截断现有文件,否则它将创建该文件。我在writekey()函数内部进行了编辑。当我检查文件时,里面什么都没有。应该总是有一条带钥匙的线。这是怎么发生的?我将在不删除文件的情况下尝试它。