是否可以强制同步 Windows 网络共享?

Is it possible to force a sync of a Windows Network Share?

我有一个用 Python 编写的服务器,它只是将请求的文件从内部存储复制到 Windows 网络共享:

import shutil
import os.path

class RPCServer(SimpleXMLRPCServer):
    def fetchFile(self, targetDir, fileName):
        try:
            shutil.copy(
                os.path.join(server_path, fileName)
                os.path.join(targetDir, fileName)
            )
            f = open(filepath, 'a')
            f.flush()
            os.fsync(f.fileno())
            f.close()
            return os.path.join(targetDir, fileName)
        except Exception, e:
            return ''

当客户端在 RPC 调用返回后尝试打开文件时,有时会失败,提示文件不可用:

class RCPClient():
    def fetchFile(self, fileName):
        filepath = server.fetchFile(targetDir, filename)
        f = open(filePath) # Exception here

怎么会?服务器中的 fsync 不确保文件可用吗?有没有办法在客户端同步网络共享上的文件夹?

fsync 只知道本地文件系统。它不可能确保任何连接的客户端都可以访问该文件。我建议您重写您的应用程序,而不是直接 return 文件。因此完全避免了同步,实际上简化了客户端和服务器。