Message9145

Author pekka.klarck
Recipients pekka.klarck
Date 2014-10-14.10:44:26
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1413283467.17.0.0214186478653.issue2221@psf.upfronthosting.co.za>
In-reply-to
Content
Title pretty much explains the problem. After studying this a bit, I was able to implement getting pid so that it works at least on Windows 7 and Linux Mint. The resulting code is at the end of this description and it can be tested by running the attached script that contains also simple code to verify the results.

If there's interest, I can take a look at turning this into a pull request. Final solution ought to have error handling at least when accessing private fields. I would assume it's better to return None than raise an exception on error.

----------8<------------------8<----------------------


import os

if os.sep == '/':

    def get_pid(process):
        field = process.getClass().getDeclaredField('pid')
        field.setAccessible(True)
        return field.getLong(process)

else:

    import ctypes
    GetProcessId = ctypes.cdll.kernel32.GetProcessId
    GetProcessId.argtypes = (ctypes.c_long,)

    def get_pid(proces):
        field = process.getClass().getDeclaredField('handle')
        field.setAccessible(True)
        handle = field.getLong(process)
        return GetProcessId(handle)
History
Date User Action Args
2014-10-14 10:44:27pekka.klarcksetrecipients: + pekka.klarck
2014-10-14 10:44:27pekka.klarcksetmessageid: <1413283467.17.0.0214186478653.issue2221@psf.upfronthosting.co.za>
2014-10-14 10:44:27pekka.klarcklinkissue2221 messages
2014-10-14 10:44:26pekka.klarckcreate