简体   繁体   English

Python os.listdir() 不返回某些文件

[英]Python os.listdir() doesn't return some files

I have a python function that checks the modified time of a folder using os.stat() and then it does a os.listdir() of that folder.我有一个 python function 使用os.stat()检查文件夹的修改时间,然后它对该文件夹执行os.listdir()

I have another process which create files in the folder being checked.我有另一个进程在被检查的文件夹中创建文件。 Sometimes it is observed that three files are created with same timestamp and the folder's stat also has the same timestamp.有时会观察到三个文件使用相同的时间戳创建,并且文件夹的 stat 也具有相同的时间戳。

When the python function fetches the the files in the same millisecond, it is observed that os.listdir() is providing only 2 of the 3 created files.当 python function 在同一毫秒内获取文件时,观察到os.listdir()仅提供 3 个创建文件中的 2 个。 Why is this so?为什么会这样?

The environment is:环境是:

OS: Red Hat Enterprise Linux Server release 7.6操作系统:红帽企业 Linux 服务器版本 7.6

Python Version: Python 3.6.8 Python 版本:Python 3.6.8

Filesystem: xfs文件系统:xfs

Sample code示例代码

import os
import sys
import time

filelist=list()
last_mtime=None


def walkover():
    path_to_check="/path/to/check"
    curr_mtime=os.stat(path_to_check).st_mtime_ns
    global last_mtime
    global filelist
    if last_mtime == None or curr_mtime > last_mtime:
        for file in os.listdir(path_to_check):
            if file not in filelist:
                filelist.append(file)
        last_mtime = curr_mtime
        print ("{} modified at {}".format(path_to_check, last_mtime))


The function is invoked to maintain a list of files at a point of time.调用 function 来维护某个时间点的文件列表。 The if case is present to avoid multiple os.listdir() invokations.存在 if 情况以避免多次os.listdir()调用。

Edit:编辑:

The files are ".rsp" files which gets created by ninja when a ".o" is about to get built.这些文件是“.rsp”文件,当“.o”即将构建时由忍者创建。

Since my machine has multiple cores (16), the ninja is triggered from cmake with "--parallel 16".由于我的机器有多个内核(16),忍者是从 cmake 用“--parallel 16”触发的。 This will cause 16 compilations to happen parallely.这将导致 16 次编译并行发生。

I don't know this ninja thing but it seems to me that when writing the first ".rst" file and running the walkover function are executed at approximately the same time the walkover function may be missing out on the creation of the other files.我不知道这个忍者的事情,但在我看来,当编写第一个“.rst”文件并运行 walkover function 时,几乎同时执行 walkover function 可能会在创建其他文件时丢失。 Wether it's buffers or queues or whatever, specifically when parallel processes are in play the order of events can be hard to grasp.无论是缓冲区还是队列或其他什么,特别是当并行进程正在运行时,事件的顺序可能很难掌握。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM