Process id error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • arunhero99
    New Member
    • Apr 2007
    • 3

    Process id error

    Hi Guys,

    Given below is the function i have defined to find process id of the running process xyz.exe. But when i try to run this function when the process xyz.exe is not running, obviously "pid = p[0].Properties_('P rocessId').Valu e" shows error as the process doesn't exist.
    but i want it to return pid="" ,to use that value to check if the process is running or not.I am now managing with a try block to catch the exception and return pid="".but this looks bit awkward.
    So would like some experts to pitch in with some idea to implement something more elegant.

    [CODE=python]from win32com.client import GetObject
    WMI = GetObject('winm gmts:')

    def getPid():
    pid = ""
    p=None
    processes = WMI.InstancesOf ('Win32_Process ')
    p = WMI.ExecQuery(' select * from Win32_Process where Name="xyz.exe"' )
    pid = p[0].Properties_('P rocessId').Valu e
    return pid[/CODE]

    Any ideas?
    Thanks,
    aRuN
    Last edited by bartonc; Jun 1 '07, 06:50 AM. Reason: added [code][/code] tags
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by arunhero99
    Hi Guys,

    Given below is the function i have defined to find process id of the running process xyz.exe. But when i try to run this function when the process xyz.exe is not running, obviously "pid = p[0].Properties_('P rocessId').Valu e" shows error as the process doesn't exist.
    but i want it to return pid="" ,to use that value to check if the process is running or not.I am now managing with a try block to catch the exception and return pid="".but this looks bit awkward.
    So would like some experts to pitch in with some idea to implement something more elegant.

    [CODE=python]from win32com.client import GetObject
    WMI = GetObject('winm gmts:')

    def getPid():
    pid = ""
    p=None
    processes = WMI.InstancesOf ('Win32_Process ')
    p = WMI.ExecQuery(' select * from Win32_Process where Name="xyz.exe"' )
    pid = p[0].Properties_('P rocessId').Valu e
    return pid[/CODE]

    Any ideas?
    Thanks,
    aRuN
    I'd probably go with:
    [CODE=python]#....
    try:
    pid = p[0].Properties_('P rocessId').Valu e
    except IndexError: # or what ever error you are getting
    pid = None[/CODE]

    Comment

    • arunhero99
      New Member
      • Apr 2007
      • 3

      #3
      Thanks for the reply...
      i am actually using try and a generic except to implement this.
      I just wanted to know if there is any other function in python which doesnt touch win32api to find process id.

      thanks again.
      Cheers...

      Comment

      • ghostdog74
        Recognized Expert Contributor
        • Apr 2006
        • 511

        #4
        just for information sharing, i use Tim golden's WMI module
        Code:
        import wmi
        c = wmi.WMI ()
        for process in c.Win32_Process ():
          print process.ProcessId, process.Name
          if process.Name=="firefox.exe":
                print "do something"

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #5
          Originally posted by arunhero99
          Thanks for the reply...
          i am actually using try and a generic except to implement this.
          I just wanted to know if there is any other function in python which doesnt touch win32api to find process id.

          thanks again.
          Cheers...
          We actually call
          Code:
          ...
              except:
                  # do something
          a naked except and consider to be too broad. For example, if the error is a memory error and you go off and do something that needs more memory, you have not helped the situation.
          Just a thought...

          Comment

          • ghostdog74
            Recognized Expert Contributor
            • Apr 2006
            • 511

            #6
            Originally posted by arunhero99
            Thanks for the reply...
            i am actually using try and a generic except to implement this.
            you can use :
            Code:
            try: ...
            except Exception,err:
               print err...
            I just wanted to know if there is any other function in python which doesnt touch win32api to find process id.

            thanks again.
            Cheers...
            no and yes.
            no means not as far as i know. yes means you can use system() or os.popen() or subprocess module to call external commands like tasklist (XP) , pslist (M$, originally sysinternals) or others to display the processes for you. Then parse the results in Python.

            Comment

            Working...