【发布时间】:2019-02-07 18:55:38
【问题描述】:
我正在解析一个不断更新的日志文件的最后一行。如果匹配,我想将匹配返回到列表并使用该数据启动另一个函数。即使新功能继续运行,我也需要继续关注新条目并解析它们。
我已经从几个不同的角度进行了大约一周的工作,并取得了不同程度的成功。我尝试了线程,但在获取返回值时遇到了问题,我尝试使用全局 var 但无法使其正常工作。我现在正在尝试 asyncio,但要使其正常工作还有更多问题。
def tail():
global match_list
f.seek(0, os.SEEK_END)
while True:
line = f.readline()
if not line:
time.sleep(0.1)
continue
yield line
def thread():
while True:
tail()
def somefun(list):
global match_list
#do things here
pass
def main():
match_list = []
f = open(r'file.txt')
thread=threading.Thread(target=thread, args=(f,))
thread.start()
while True:
if len(match_list) >= 1:
somefun(match_list)
if __name__ == '__main__':
main()
以上是凭记忆写的。。 我希望 tail() 将该行返回到 somefun() 可以使用的列表。 我在让它工作时遇到问题,我将使用线程或 asyncio.. 任何东西让它在这一点上运行。
【问题讨论】:
标签: python-3.x python-asyncio python-multithreading