在 Windows 上使用 pyqt 时,QProcess.pid() 结果代表什么? [英] When using pyqt on Windows, what does the QProcess.pid() result represent?

查看:35
本文介绍了在 Windows 上使用 pyqt 时,QProcess.pid() 结果代表什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这显示我的 PID 为 8304.不过,我的应用程序中的 print 语句显示:

PID:44353984

这代表什么?它与任务管理器报告的 8304 PID 相比如何?

解决方案

在 Unix 系统上,pid 将是一个 qint64,但在 Windows 上它将是 struct 之类的这个:

typedef struct _PROCESS_INFORMATION {处理 hProcess;处理 hThread;DWORD dwProcessId;DWORD dwThreadId;} PROCESS_INFORMATION, *LPPROCESS_INFORMATION;

PyQt 将为这样的结构返回一个 sip.voidptr,这就是为什么当您使用 int() 转换它时会看到这个奇怪的值.您想要的实际 pid 是 dwProcessId,因此您需要使用类似 ctypes 的东西来提取它.

这里是一些完全未经测试的代码,可以完成这项工作:

import ctypes类 WinProcInfo(ctypes.Structure):_字段_ = [('hProcess', ctypes.wintypes.HANDLE),('hThread', ctypes.wintypes.HANDLE),('dwProcessID', ctypes.wintypes.DWORD),('dwThreadID', ctypes.wintypes.DWORD),]LPWinProcInfo = ctypes.POINTER(WinProcInfo)lp = ctypes.cast(int(self.process.pid()), LPWinProcInfo)打印(lp.contents.dwProcessID)

The documentation for QProcess.pid() says:

Returns the native process identifier for the running process, if available. If no process is currently running, 0 is returned.

What does this mean?

This code is used to explain my confusion. I am using Python 2.7.9, PyQt 4 and Windows 7:

import  sys, os, time
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class testLaunch(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        self.process = QProcess(self)
        self.process.start('calc')
        self.process.waitForStarted(1000)
        print "PID:", int(self.process.pid())

if __name__ == "__main__":
    app = QApplication(sys.argv)
    main = testLaunch()
    main.show()
    sys.exit(app.exec_())

This launches the Windows calculator application, as expected. In the task manager, it shows the following:

This shows my PID as 8304. The print statement from my application though, shows:

PID: 44353984

What does this represent and how does it compare to the 8304 PID the task manager reports?

解决方案

On Unix systems, the pid will be a qint64, but on Windows it will be struct like this:

typedef struct _PROCESS_INFORMATION {
  HANDLE hProcess;
  HANDLE hThread;
  DWORD  dwProcessId;
  DWORD  dwThreadId;
} PROCESS_INFORMATION, *LPPROCESS_INFORMATION;

PyQt will return a sip.voidptr for such a struct, which is why you are seeing that weird value when you convert it with int(). The actual pid you want is the dwProcessId, so you will need to use something like ctypes to extract it.

Here is some completely untested code which might do the job:

import ctypes

class WinProcInfo(ctypes.Structure):
    _fields_ = [
        ('hProcess', ctypes.wintypes.HANDLE),
        ('hThread', ctypes.wintypes.HANDLE),
        ('dwProcessID', ctypes.wintypes.DWORD),
        ('dwThreadID', ctypes.wintypes.DWORD),
        ]
LPWinProcInfo = ctypes.POINTER(WinProcInfo)

lp = ctypes.cast(int(self.process.pid()), LPWinProcInfo)
print(lp.contents.dwProcessID)

这篇关于在 Windows 上使用 pyqt 时,QProcess.pid() 结果代表什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆