我正在运行几个脚本,这些脚本通过网络共享从同一个CSV文件中写入和读取。
我正在间歇OSError: [Errno 2] No such file or directory: 'filename.csv'
(每隔一两分钟,每10-30秒写一次/读一次),但大部分时间文件写/读都能正常工作。
有没有办法跟踪哪个脚本打开文件,并获得有关错误的更多详细信息?即是正在使用的文件?是否失去了与文件服务器的连接?
普通操作(无错误信息):
2:33:52.4823608 PM python.exe 2668 IRP_MJ_QUERY_VOLUME_INFORMATION filename.csv SUCCESS Type: QueryDeviceInformationVolume, DeviceType: Disk, Characteristics: Remote
2:33:52.4823848 PM python.exe 2668 IRP_MJ_QUERY_VOLUME_INFORMATION filename.csv SUCCESS Type: QueryDeviceInformationVolume, DeviceType: Disk, Characteristics: Remote
2:33:52.4824062 PM python.exe 2668 IRP_MJ_QUERY_VOLUME_INFORMATION filename.csv BUFFER OVERFLOW Type: QueryInformationVolume, VolumeCreationTime: 1/1/1601 8:00:00 AM, VolumeSerialNumber: D020-FD78, SupportsObjects: False, VolumeLabel: DAT`
2:33:52.4827061 PM python.exe 2668 IRP_MJ_QUERY_INFORMATION filename.csv BUFFER OVERFLOW Type: QueryAllInformationFile, CreationTime: 4/24/2012 2:36:07 PM, LastAccessTime: 4/24/2012 2:36:07 PM, LastWriteTime: 4/24/2012 2:36:07 PM, ChangeTime: 4/24/2012 2:36:07 PM, FileAttributes: A, AllocationSize: 1,048,576, EndOfFile: 118,342, NumberOfLinks: 1, DeletePending: False, Directory: False, IndexNumber: 0x45060de, EaSize: 0, Access: None, Position: 0, Mode: , AlignmentRequirement: Byte
2:33:52.4837725 PM python.exe 2668 FASTIO_QUERY_INFORMATION filename.csv FAST IO DISALLOWED Type: QueryStandardInformationFile
2:33:52.4837820 PM python.exe 2668 IRP_MJ_QUERY_INFORMATION filename.csv SUCCESS Type: QueryStandardInformationFile, AllocationSize: 1,048,576, EndOfFile: 118,342, NumberOfLinks: 1, DeletePending: False, Directory: False
2:33:52.4841324 PM python.exe 2668 FASTIO_QUERY_INFORMATION filename.csv FAST IO DISALLOWED Type: QueryStandardInformationFile
2:33:52.4841458 PM python.exe 2668 IRP_MJ_QUERY_INFORMATION filename.csv SUCCESS Type: QueryStandardInformationFile, AllocationSize: 1,048,576, EndOfFile: 118,342, NumberOfLinks: 1, DeletePending: False, Directory: False
2:33:52.4844948 PM python.exe 2668 IRP_MJ_READ filename.csv SUCCESS Offset: 117,318, Length: 1,024
2:33:52.4857179 PM python.exe 2668 IRP_MJ_READ filename.csv END OF FILE Offset: 118,342, Length: 4,096
2:33:52.4862472 PM python.exe 2668 FASTIO_QUERY_INFORMATION filename.csv FAST IO DISALLOWED Type: QueryStandardInformationFile
2:33:52.4862564 PM python.exe 2668 IRP_MJ_QUERY_INFORMATION filename.csv SUCCESS Type: QueryStandardInformationFile, AllocationSize: 1,048,576, EndOfFile: 118,342, NumberOfLinks: 1, DeletePending: False, Directory: False
2:33:52.4867251 PM python.exe 2668 IRP_MJ_READ filename.csv SUCCESS Offset: 116,294, Length: 1,024
2:33:52.4873473 PM python.exe 2668 IRP_MJ_READ filename.csv SUCCESS Offset: 117,318, Length: 512
2:33:52.4878825 PM python.exe 2668 FASTIO_QUERY_INFORMATION filename.csv FAST IO DISALLOWED Type: QueryStandardInformationFile
2:33:52.4878917 PM python.exe 2668 IRP_MJ_QUERY_INFORMATION filename.csv SUCCESS Type: QueryStandardInformationFile, AllocationSize: 1,048,576, EndOfFile: 118,342, NumberOfLinks: 1, DeletePending: False, Directory: False
2:33:52.4882153 PM python.exe 2668 IRP_MJ_READ filename.csv SUCCESS Offset: 115,270, Length: 1,024
2:33:52.4889601 PM python.exe 2668 IRP_MJ_READ filename.csv SUCCESS Offset: 116,294, Length: 512
2:33:52.4895164 PM python.exe 2668 IRP_MJ_CLEANUP filename.csv SUCCESS
2:33:52.4895513 PM python.exe 2668 IRP_MJ_CLOSE filename.csv SUCCESS
文件在另一个程序中打开(Errno 13 - 权限被拒绝):
1:12:42.0840918 PM python.exe 5772 IRP_MJ_CREATE filename.csv SHARING VIOLATION Desired Access: Generic Write, Read Attributes, Disposition: OpenIf, Options: Synchronous IO Non-Alert, Non-Directory File, Attributes: N, ShareMode: Read, Write, AllocationSize: 0
其他错误(不确定这是否与python错误相对应)
2:57:59.9371101 PM python.exe 3584 IRP_MJ_CREATE filename.log BAD NETWORK PATH Desired Access: Generic Read, Disposition: Open, Options: Synchronous IO Non-Alert, Non-Directory File, Attributes: N, ShareMode: Read, Write, AllocationSize: n/a
这会显示为OSError 2吗?
答案 0 :(得分:1)
如果您使用的是Windows,请使用Process Monitor记录Python.exe进程完成的所有文件操作。如果您使用的是Mac OS X或Solaris,请使用DTrace。如果您使用的是Linux,请使用strace。
这些工具中的任何一个都会为您提供所有正在进行的系统调用的详细列表,以及它们的返回代码,从而可以缩小失败的范围以及可能的原因。
答案 1 :(得分:0)
作为一种解决方法,您可以使用异常处理(try / except)包装代码
真正的解决方案更复杂 - 同步脚本。 这就是我要做的: 编写一个侦听命令的代理脚本(例如UDP或SimpleXMLRPCServer)。其他脚本与之通信。共享文件上的所有操作都是通过代理完成的。
答案 2 :(得分:0)
如何创建ftp共享?如何组织'开放'文件继续进行?
a)尝试根据pep 343使用'with'。
如果不起作用:
b)尝试使用两种方法制作自己的ftp共享服务器 - 读取和
写。你可以实现non-blocking reading算法。
非阻塞算法在中断处理程序中也是安全的: 即使抢占的线程无法恢复,进展仍然存在 可能没有它。相比之下,全球数据结构受到保护 互斥不能安全地在处理程序中访问,因为 抢占线程可能是持有锁的人。