[python-win32] win32process open-terminate loop cripples OS

David S. david128 at receptec.net
Mon Mar 20 15:42:24 CET 2006


Hello,

I decided to attempt to use python's win32 extensions as a method to
monitor and control process for my python application.  It's working
well in the short term, but, as this app is intended to be very
low-maintenance, and probably high-uptimes without restarting, I want to
put it to the test.  I set a loop similar to below to run every second
for the weekend.  It wasn't very pretty.  I didn't get any process
information (couldn't - system was /almost/ unresponsive) - the most i
could do was alt-tab around, though it wouldn't paint the window, move
the mouse pointer and turn the num lock light on and off.  Can anyone
provide some guidance or perhaps point out a problem in my code?

For what it's worth, I think I can poll process /ad infinitum/ without
any lockups.

Thank you,

-David S.

---------------------------------------------------------------------------

def weekendprocesstest():
    print 1
    Processes.StartProcess("notepad")
    print 2
    Processes.KillName("notepad")
    print 3


---and the functions....


def StartProcess(self, RunCmd):
        #not going to bother with win32 for this
        #output=os.System(RunCmd).read()
        StartupInfo = win32process.STARTUPINFO()
        win32process.CreateProcess(
                None,   # program
                RunCmd, # command line
                None,   # process security attributes
                None,   # thread attributes
                1,      # inherit handles, or USESTDHANDLES won't work.
                        # creation flags. Don't access the console.
                0,      # Don't need anything here.
                        # If you're in a GUI app, you should use
                        # CREATE_NEW_CONSOLE here, or any subprocesses
                        # might fall victim to the problem described in:
                        # KB article: Q156755, cmd.exe requires
                        # an NT console in order to perform redirection..
                None,   # new environment
                None,          # new directory
                StartupInfo)
        self.ProcessTableRefresh()


def KillName(self, matchstring):
        #get PIDS that have the supplied substring
        self.ProcessTableRefresh()
        ProcessList=self.GetProcessList()
        for process in ProcessList:
            if process[2].lower().find(matchstring.lower())!=-1:
                win32process.TerminateProcess(process[0],0)
        self.ProcessTableRefresh()

def ProcessTableRefresh(self):
        processes = win32process.EnumProcesses()
        pnamepidlist=list()
        for pid in processes:
            try:
                handle =
win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS,False, pid)
                exe = win32process.GetModuleFileNameEx(handle, 0)
                pnamepidlist.append((handle, pid, exe))
            except:
                pass

        self.lProcessTable.acquire()
        self.ProcessList=pnamepidlist
        self.ProcessTableAge=0
        self.lProcessTable.release()




More information about the Python-win32 mailing list