- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
使用Python watchdog文件系统事件监视库我注意到,在 Windows Server 2003 下使用时,它会进入“轮询模式”,从而停止使用异步操作系统通知,因此,在大量文件更改下会严重降低系统性能。
我将问题追溯到watchdog/observers/winapi.py
文件在哪里 CancelIoEx
使用系统调用来停止 ReadDirectoryChangesW
当用户想要停止监视被监视的目录或文件时调用lock:
(winapi.py)
CancelIoEx = ctypes.windll.kernel32.CancelIoEx
CancelIoEx.restype = ctypes.wintypes.BOOL
CancelIoEx.errcheck = _errcheck_bool
CancelIoEx.argtypes = (
ctypes.wintypes.HANDLE, # hObject
ctypes.POINTER(OVERLAPPED) # lpOverlapped
)
...
...
...
def close_directory_handle(handle):
try:
CancelIoEx(handle, None) # force ReadDirectoryChangesW to return
except WindowsError:
return
CancelIoEx
的问题调用是它直到 Windows Server 2008 才可用: http://msdn.microsoft.com/en-us/library/windows/desktop/aa363792(v=vs.85).aspx
一种可能的替代方案是更改 close_directory_handle
为了使其在受监视的目录中创建一个模拟文件,从而解锁等待 ReadDirectoryChangesW
的线程返回。
但是,我注意到CancelIo
系统调用是 in fact available在 Windows Server 2003 中:
Cancels all pending input and output (I/O) operations that are issued by the calling thread for the specified file. The function does not cancel I/O operations that other threads issue for a file handle. To cancel I/O operations from another thread, use the CancelIoEx function.
但是打电话CancelIo
不会影响等待线程。
您知道如何解决这个问题吗?可能是threading.enumerate()
可以用来发出由每个线程处理的信号 CancelIo
从这些处理程序调用?
最佳答案
自然的方法是实现一个完成例程并使用其重叠模式调用ReadDirectoryChangesW
。以下示例展示了执行此操作的方法:
RDCW_CALLBACK_F = ctypes.WINFUNCTYPE(None, ctypes.wintypes.DWORD, ctypes.wintypes.DWORD, ctypes.POINTER(OVERLAPPED))
首先,创建一个 WINFUNCTYPE 工厂,它将用于从 python 方法生成(可从 Windows API 调用)类似 C 的函数。本例中没有返回值,对应3个参数
VOID CALLBACK FileIOCompletionRoutine(
_In_ DWORD dwErrorCode,
_In_ DWORD dwNumberOfBytesTransfered,
_Inout_ LPOVERLAPPED lpOverlapped
);
FileIOCompletionRoutine header 。
需要将回调引用以及重叠结构添加到 ReadDirectoryChangesW
参数列表中:
ReadDirectoryChangesW = ctypes.windll.kernel32.ReadDirectoryChangesW
ReadDirectoryChangesW.restype = ctypes.wintypes.BOOL
ReadDirectoryChangesW.errcheck = _errcheck_bool
ReadDirectoryChangesW.argtypes = (
ctypes.wintypes.HANDLE, # hDirectory
LPVOID, # lpBuffer
ctypes.wintypes.DWORD, # nBufferLength
ctypes.wintypes.BOOL, # bWatchSubtree
ctypes.wintypes.DWORD, # dwNotifyFilter
ctypes.POINTER(ctypes.wintypes.DWORD), # lpBytesReturned
ctypes.POINTER(OVERLAPPED), # lpOverlapped
RDCW_CALLBACK_F # FileIOCompletionRoutine # lpCompletionRoutine
)
从这里开始,我们准备好执行重叠的系统调用。这是一个简单的 bacl 调用,可用于测试一切是否正常:
def dir_change_callback(dwErrorCode,dwNumberOfBytesTransfered,p):
print("dir_change_callback! PID:" + str(os.getpid()))
print("CALLBACK THREAD: " + str(threading.currentThread()))
准备并执行通话:
event_buffer = ctypes.create_string_buffer(BUFFER_SIZE)
nbytes = ctypes.wintypes.DWORD()
overlapped_read_dir = OVERLAPPED()
call2pass = RDCW_CALLBACK_F(dir_change_callback)
hand = get_directory_handle(os.path.abspath("/test/"))
def docall():
ReadDirectoryChangesW(hand, ctypes.byref(event_buffer),
len(event_buffer), False,
WATCHDOG_FILE_NOTIFY_FLAGS,
ctypes.byref(nbytes),
ctypes.byref(overlapped_read_dir), call2pass)
print("Waiting!")
docall()
如果您将所有这些代码加载并执行到 DreamPie 中交互式 shell 您可以检查系统调用是否完成以及回调是否执行,从而在 c:\test
目录下完成第一次更改后打印线程和 pid 号。此外,您会注意到它们与主线程和进程相同:尽管事件是由单独的线程引发的,但回调在与我们的主程序相同的进程和线程中运行,从而提供了不期望的行为:
lck = threading.Lock()
def dir_change_callback(dwErrorCode,dwNumberOfBytesTransfered,p):
print("dir_change_callback! PID:" + str(os.getpid()))
print("CALLBACK THREAD: " + str(threading.currentThread()))
...
...
...
lck.acquire()
print("Waiting!")
docall()
lck.acquire()
该程序将锁定主线程并且回调将永远不会执行。我尝试了很多同步工具,甚至 Windows API 信号量也总是得到相同的行为,所以最后,我决定在使用 管理和同步的单独进程中使用
python 库:ReadDirectoryChangesW
的同步配置来实现异步调用多处理
调用 get_directory_handle
不会返回 Windows API 给出的句柄编号,而是返回由 winapi
库管理的句柄编号,为此我实现了一个句柄生成器:
class FakeHandleFactory():
_hl = threading.Lock()
_next = 0
@staticmethod
def next():
FakeHandleFactory._hl.acquire()
ret = FakeHandleFactory._next
FakeHandleFactory._next += 1
FakeHandleFactory._hl.release()
return ret
每个生成的句柄必须与文件系统路径全局关联:
handle2file = {}
每次调用read_directory_changes
现在都会生成ReadDirectoryRequest
(派生自multiprocessing.Process
)对象:
class ReadDirectoryRequest(multiprocessing.Process):
def _perform_and_wait4request(self, path, recursive, event_buffer, nbytes):
hdl = CreateFileW(path, FILE_LIST_DIRECTORY, WATCHDOG_FILE_SHARE_FLAGS,
None, OPEN_EXISTING, WATCHDOG_FILE_FLAGS, None)
#print("path: " + path)
aux_buffer = ctypes.create_string_buffer(BUFFER_SIZE)
aux_n = ctypes.wintypes.DWORD()
#print("_perform_and_wait4request! PID:" + str(os.getpid()))
#print("CALLBACK THREAD: " + str(threading.currentThread()) + "\n----------")
try:
ReadDirectoryChangesW(hdl, ctypes.byref(aux_buffer),
len(event_buffer), recursive,
WATCHDOG_FILE_NOTIFY_FLAGS,
ctypes.byref(aux_n), None, None)
except WindowsError as e:
print("!" + str(e))
if e.winerror == ERROR_OPERATION_ABORTED:
nbytes = 0
event_buffer = []
else:
nbytes = 0
event_buffer = []
# Python 2/3 compat
nbytes.value = aux_n.value
for i in xrange(self.int_class(aux_n.value)):
event_buffer[i] = aux_buffer[i]
CloseHandle(hdl)
try:
self.lck.release()
except:
pass
def __init__(self, handle, recursive):
buffer = ctypes.create_string_buffer(BUFFER_SIZE)
self.event_buffer = multiprocessing.Array(ctypes.c_char, buffer)
self.nbytes = multiprocessing.Value(ctypes.wintypes.DWORD, 0)
targetPath = handle2file.get(handle, None)
super(ReadDirectoryRequest, self).__init__(target=self._perform_and_wait4request, args=(targetPath, recursive, self.event_buffer, self.nbytes))
self.daemon = True
self.lck = multiprocessing.Lock()
self.result = None
try:
self.int_class = long
except NameError:
self.int_class = int
if targetPath is None:
self.result = ([], -1)
def CancelIo(self):
try:
self.result = ([], 0)
self.lck.release()
except:
pass
def read_changes(self):
#print("read_changes! PID:" + str(os.getpid()))
#print("CALLBACK THREAD: " + str(threading.currentThread()) + "\n----------")
if self.result is not None:
raise Exception("ReadDirectoryRequest object can be used only once!")
self.lck.acquire()
self.start()
self.lck.acquire()
self.result = (self.event_buffer, self.int_class(self.nbytes.value))
return self.result
此类指定Process
,提供执行系统调用并等待直到(或)的进程:
ReadDirectoryRequest
对象 CancelIo
方法取消请求。请注意:
角色现在用于管理请求。为此,需要线程锁和辅助数据结构:
rqIndexLck = threading.Lock() # Protects the access to `rqIndex`
rqIndex = {} # Maps handles to request objects sets.
获取目录句柄
def get_directory_handle(path):
rqIndexLck.acquire()
ret = FakeHandleFactory.next()
handle2file[ret] = path
rqIndexLck.release()
return ret
close_directory_handle
def close_directory_handle(handle):
rqIndexLck.acquire()
rqset4handle = rqIndex.get(handle, None)
if rqset4handle is not None:
for rq in rqset4handle:
rq.CancelIo()
del rqIndex[handle]
if handle in handle2file:
del handle2file[handle]
rqIndexLck.release()
最后但并非最不重要的一点:read_directory_changes
def read_directory_changes(handle, recursive):
rqIndexLck.acquire()
rq = ReadDirectoryRequest(handle, recursive)
set4handle = None
if handle in rqIndex:
set4handle = rqIndex[handle]
else:
set4handle = set()
rqIndex[handle] = set4handle
set4handle.add(rq)
rqIndexLck.release()
ret = rq.read_changes()
rqIndexLck.acquire()
if rq in set4handle:
set4handle.remove(rq)
rqIndexLck.release()
return ret
关于python - 看门狗兼容性 : A workaround for "CancelIoEx",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25669139/
ATtiny88初体验(四):看门狗 ATtiny88单片机的看门狗使用内部独立的128KHz时钟源,拥有3种工作模式: Interrupt模式:超时产生中断; Syste
1、前言 假设现在有一个应用场景,需要对文件系统进行监控,发生变化时产生日志,对新增的文件做一些相应的操作。 比如说应用到我们之前的音乐高潮提取器:若当前文件夹下增加了一个音乐文件,监控器就调
我正在尝试编写一个程序,该程序可以检测是否已插入或拔出了耳机,并因此执行了某些操作。我使用的是Ubuntu 14.04,经过一番搜索,我发现耳机/耳机的连接状态已写入此路径下的编解码器文件-/ pro
我是 Python 的新手,我正在尝试实现良好的“文件创建”检测。如果我不放置 time.sleep(x),我的文件将以错误的方式详细说明,因为它们仍在文件夹中“创建”。 (缓冲区不为空)我如何在每次
Linux 上是否有用于以下目的的看门狗工具或库?我想构建一个看门狗可执行文件,它启动 2 个进程并在以下情况下重新启动它们: 进程崩溃 进程变得无响应(例如由于某种原因挂起) 互联网搜索找到了 wa
我正在尝试从 C 向 Raspbian 中的看门狗设备 - /dev/watchdog 提供数据。 问题是无论我如何尝试访问设备总是抛出忙碌或权限被拒绝的错误(因为进程已经在运行并由系统提供.....
嗨,我正在考虑使用 Python 在 Windows XP 上制作一个看门狗应用程序,它将执行以下操作: 在给定时间重新启动 Windows。 启动一个 exe 应用程序。 运行计时器以检查:应用程序
我打算使用 Python 看门狗来处理写入文件的目录,我只对图像文件感兴趣,麻烦的是我不太理解 this page 上的代码.这是我的尝试: from watchdog.observers impor
我正在使用 Watchdog监视网络目录,非递归的,随着时间的推移要创建的特定文件模式。我看到的问题是,虽然它在本地测试时效果很好,但如果我从远程计算机更改受监控的目录,则不会触发事件。 这里是我配置
Watchdog 非常棒,可以让您对特定目录进行递归快照。它甚至可以让您使用名为 DirectorySnapshotDiff 的函数比较快照. 我的程序会实时观察目录的变化,因此需要使用此函数的输出。
我在 threaded 进程环境中长时间运行 boost::regex_match(...) 调用时遇到问题。但它可能是另一个具有相同问题的库(API 调用)。 有没有通用的方法来设置看门狗? 对于非
我正在 Raspberry Pi 上开发一个系统,该系统使用 Web 界面与之交互,并具有自定义 C++ 服务器。这个想法是,您只需插入 Pi,不需要手动启动服务器。虽然这不是问题,但服务器有时会出现
我正在尝试实现一个看门狗,它将 ping 主机,如果它已启动,那么它将让 midori 打开远程页面,如果没有,那么它将打开本地页面。 我已根据此处的代码进行了改编:https://raspberry
我有一个无限期运行的实时数据采集器,每隔几秒通过 HTTP 采集数据并将其放入 MySQL 数据库中。 在我的程序中,我有一个 while True 循环,每当最后生成的时间大于 X 秒时,它就会生成
之前我是在Windows系统上开发OpenCl内核,现在我用的是Linux。在 Windows 上,有一个名为 watchdog 的巧妙功能,如果它持续超过 5 秒(默认情况下),它会自动停止 Ope
我正在使用 Watchdog 来监控目录并使其与 Dropbox 保持同步。 我遇到这样一种情况,每次我从 Dropbox 下载文件时,我都会触发一个上传事件,因为我需要写入 Watchdog 正在监
现在我遇到一个关于 Linux NMI Watchdog 的问题。我想使用 Linux NMI 看门狗来检测和恢复操作系统挂起。因此,我将“nmi_watchdog=1”添加到 grub.cfg 中。
我已经在 Ubuntu 10.04 上使用 Apache2 完成了 Phusion Passenger 设置。每当我重新启动 Apache 服务器时,我都会收到以下错误消息,并且 Rails 应用程序
我有一个运行 Debian 的 Beaglebone Black,我需要一个在后台运行的脚本,它会每隔几秒检查一次系统是否仍在运行。据我了解,BBB 中的看门狗是基于硬件的,独立于操作系统运行,因此即
我正在尝试使用 passenger 和 nginx 运行 rails。 Nginx 不会重定向到我的 Rails 应用程序,而是继续到其默认的 index.html 页面,我得到了这个错误: Unab
我是一名优秀的程序员,十分优秀!