Python 是否可以强制同步Windows网络共享?
我有一个用Python编写的服务器,它只将请求的文件从内部存储复制到Windows网络共享:Python 是否可以强制同步Windows网络共享?,python,smb,Python,Smb,我有一个用Python编写的服务器,它只将请求的文件从内部存储复制到Windows网络共享: import shutil import os.path class RPCServer(SimpleXMLRPCServer): def fetchFile(self, targetDir, fileName): try: shutil.copy( os.path.join(server_path, fileName)
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只知道本地文件系统。它不可能确保任何连接的客户端都可以访问该文件。我建议您重写应用程序,直接返回文件。这样就完全避免了同步,实际上简化了客户机和服务器