1

我正在运行几个脚本,这些脚本通过网络共享从同一个 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 - Permission Denied):

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 吗?

4

3 回答 3

1

如果您使用的是 Windows,请使用Process Monitor记录 Python.exe 进程完成的所有文件操作。如果您使用的是 Mac OS X 或 Solaris,请使用DTrace。如果您使用的是 Linux,请使用strace

这些工具中的任何一个都会为您提供正在执行的所有系统调用的详细列表以及它们的返回代码,从而使您可以缩小失败的范围以及可能的原因。

于 2012-05-15T04:51:57.427 回答
0

作为一种解决方法,您可以使用异常处理 (try/except) 包装代码

真正的解决方案更复杂 - 同步脚本。这就是我要做的:编写一个监听命令(例如 UDP 或 SimpleXMLRPCServer)的代理脚本。其他脚本与之通信。对共享文件的所有操作都是通过代理完成的。

于 2012-04-24T05:38:25.353 回答
0

ftp 共享是如何创建的?组织的“打开”文件是如何进行的?

a) 尝试根据pep 343使用“with” 。

如果它不起作用:

b) 尝试使用两种方法制作您自己的 ftp 共享服务器 - 读取和
写入。您可以实现非阻塞读取算法。

非阻塞算法也可以安全地用于中断处理程序:即使被抢占的线程无法恢复,但没有它仍然可以继续进行。相反,受互斥保护的全局数据结构不能在处理程序中安全地访问,因为抢占线程可能是持有锁的线程。

于 2012-05-18T08:08:59.940 回答