首页 > 解决方案 > OpenProcess 在 wait_cpu_usage_lower 中失败

问题描述

我们正在尝试在某个路径打开资源管理器窗口,然后等待准备就绪。客户端通过rpyc奇怪的堆栈跟踪进行操作。这有时会发生,但仅在同一个测试中。此功能 ( fileman_item_properties) 旨在打开文件属性窗口,它在许多不同的地方都能正常工作。

    command = f'explorer.exe'
    Timings.slow()

    try:
        Application().start(f'{command} /select,{item}')
        app = Application(backend='uia').connect(path=command)
        app.wait_cpu_usage_lower(threshold=0.5, timeout=60, usage_interval=1.0)
4 Traceback (most recent call last):
265   File "/home/serj/work/mpksoft/te/tests/test_vss_simple.py", line 540, in test_restore_root_share_dirs
266     fileman_restore_item(client, self._share_path, version)
267   File "/home/serj/work/mpksoft/te/lib/host/coreui.py", line 377, in fileman_restore_item
268     host_plugin_load(conn, 'coreui').fileman_restore_item(path, version)
269   File "/home/serj/work/.virtualenvs/te36/lib/python3.6/site-packages/rpyc/core/netref.py", line 240, in __call__
270     return syncreq(_self, consts.HANDLE_CALL, args, kwargs)
271   File "/home/serj/work/.virtualenvs/te36/lib/python3.6/site-packages/rpyc/core/netref.py", line 63, in syncreq
272     return conn.sync_request(handler, proxy, *args)
273   File "/home/serj/work/.virtualenvs/te36/lib/python3.6/site-packages/rpyc/core/protocol.py", line 473, in sync_request
274     return self.async_request(handler, *args, timeout=timeout).value
275   File "/home/serj/work/.virtualenvs/te36/lib/python3.6/site-packages/rpyc/core/async_.py", line 102, in value
276     raise self._obj
277 rpyc.core.vinegar/pywintypes._get_exception_class.<locals>.Derived: (87, 'OpenProcess', 'The parameter is incorrect.')
278 
279 ========= Remote Traceback (1) =========
280 Traceback (most recent call last):
281   File "C:\Program Files\Python38\lib\site-packages\rpyc\core\protocol.py", line 320, in _dispatch_request
282     res = self._HANDLERS[handler](self, *args)
283   File "C:\Program Files\Python38\lib\site-packages\rpyc\core\protocol.py", line 593, in _handle_call
284     return obj(*args, **dict(kwargs))
285   File ".\windows\coreui.py", line 1285, in fileman_restore_item
286     fileman_item_properties(path)
287   File ".\windows\coreui.py", line 422, in fileman_item_properties
288     app.wait_cpu_usage_lower(threshold=0.5, timeout=60, usage_interval=1.0)
289   File "C:\Program Files\Python38\lib\site-packages\pywinauto\application.py", line 1135, in wait_cpu_usage_lower
290     while self.cpu_usage(usage_interval) > threshold:
291   File "C:\Program Files\Python38\lib\site-packages\pywinauto\application.py", line 1110, in cpu_usage
292     h_process = win32api.OpenProcess(win32con.MAXIMUM_ALLOWED, 0, self.process)
293 pywintypes.error: (87, 'OpenProcess', 'The parameter is incorrect.')

UPD

不幸的是,下面的代码没有解决问题。未就绪的进程以某种方式通过了connect超时。

      Application().start(f'{command} /select,{item}')
        app = Application(backend='uia').connect(path=command, title_re=f'.*{base_name}.*', timeout=60)
        app.wait_cpu_usage_lower(threshold=0.5, timeout=60, usage_interval=1.0)

顺便说一句,在出错的那一刻,Explorer 窗口尚未显示。

标签: pywinautowindows-server-2019

解决方案


有时explorer.exe没有足够的时间开始。您需要对connect()方法使用显式超时:

app = Application(backend='uia').connect(path=command, timeout=10)

另一种可能的情况是已经存在的explorer.exe实例已经在运行并且通过可执行文件名称连接是间歇性的。请使用按标题连接!

app = Application(backend='uia').connect(title="Properties", timeout=10)

Readme.md 中的标准示例也可能有帮助:https ://github.com/pywinauto/pywinauto/tree/master#ms-ui-automation-example


推荐阅读