Python 为什么os.path.getmtime()总是运行两次?这没有任何意义

Python 为什么os.path.getmtime()总是运行两次?这没有任何意义,python,asynchronous,last-modified,Python,Asynchronous,Last Modified,代码如下: import os import asyncio async def func_placing_sell_orders(): prev_final_stocks_list_state = os.path.getmtime('stock_data//final_stocks_list.json') print('i run once') while True: if (prev_final_stocks_list_state != os.path

代码如下:

import os
import asyncio
async def func_placing_sell_orders():
    prev_final_stocks_list_state = os.path.getmtime('stock_data//final_stocks_list.json')
    print('i run once')
    while True:
        if (prev_final_stocks_list_state != os.path.getmtime('stock_data//final_stocks_list.json')):
            prev_final_stocks_list_state = os.path.getmtime('stock_data//final_stocks_list.json')
            print('here')

asyncio.get_event_loop().run_until_complete(func_placing_sell_orders())
简化版本:

import os
def simple():
    state = os.path.getmtime('file.json')
    print('i run once')
    while True:
        if (state != os.path.getmtime('file.json')):
            state = os.path.getmtime('file.json')
            print('here')

simple()
这是打印件:

i run once
here
here
在这里,每次保存文件都会打印两次。我运行以检查上一次修改时间和当前修改时间之间的时间,它总是不同的,这意味着每次保存只应运行一次


这太基本了,我不明白为什么我会得到这个结果。请发送帮助

如果文件足够大,那么这里的第一个可能是在文件仍在编写编辑时,最后一个是在保存完成后。此外,如果您正在使用类似openfile、w或类似的东西来编写编辑,则首先在此处清除文件,然后在此处使用新数据进行编辑


你可以忽略太快的报告我有点惊讶它只运行了两次,因为它是在一个无限循环中被调用的。。。我也不明白你为什么要使用异步函数,你的函数中没有异步。这不是一个响应,这是一个注释,说明代码与报告的错误不匹配,这意味着请求帮助是不一致的。此外,示例代码根本无法运行。当有10%的相关信息存在而90%的信息缺失时,帮助解决问题是很困难的。@乞讨的初学者尽量礼貌一点。您是请求帮助的人。其他内容正在修改文件。当我运行您的简化示例时,如所示,while True替换为for uu in range10,我根本看不到打印的“here”。这是正确的答案。我不知道文件大小会改变这样的行为。是否有一种缓解方法,即确保只运行一次?添加了可能的解决方案
lastEdit = time.time()
while True:
    if (state != os.path.getmtime('file.json')):
        state = os.path.getmtime('file.json')
        if time.time()-lastEdit > 1: 
            print('here')
        lastEdit = time.time()