我需要一个检查文件更改的函数。
每次它发现一个变化,它就会启动一个线程并将流返回给调用者。你知道吗
def if_file_changes(file_loc):
import threading, time
with open(file_loc, 'r') as nctn:
state = nctn.read()
while True:
time.sleep(5)
with open(file_loc, 'r') as nctn:
check_state = nctn.read()
if check_state != state:
state = check_state
check_state = None
t = threading.Thread(return) # How do I return in a thread?
t.daemon = True
t.start()
编辑:
我不明白这个问题。我试图在函数级创建线程,而它本应在调用者处创建。
我的解决方案如下。你知道吗
通过在新线程中设置全局值,可以实现返回线程:
如果只有几个线程可以从中获取返回值,那么这是最简单的。你知道吗
此外,还可以将param传递给thread函数,并在线程退出之前在新线程中设置它:
必须使用list来返回值,因为在python中只有list和dict是可变的。你知道吗
PS:你真的需要阅读整个文件来检查是否有一些变化吗?检查时间戳对于检测文件更改更为常见。你知道吗
我所做的更正是将调用者模块化为函数,以便根据需要重复任何部分。然后导入文件检查器并在需要时调用它。你知道吗
函数:
相关问题 更多 >
编程相关推荐