Search code examples
pythonmultithreadingvb6event-handling

Python Equivalent of VB6 DoEvents


Is there Python equivalent of VB6 DoEvents statement where the handler will temporarily pass to the OS? I found similar code in http://code.activestate.com/recipes/496767-set-process-priority-in-windows/

def setpriority(pid=None,priority=1):
    """ Set The Priority of a Windows Process.  Priority is a value between 0-5 where
        2 is normal priority.  Default sets the priority of the current
        python process but can take any valid process ID. """

    import win32api,win32process,win32con

    priorityclasses = [win32process.IDLE_PRIORITY_CLASS,
                       win32process.BELOW_NORMAL_PRIORITY_CLASS,
                       win32process.NORMAL_PRIORITY_CLASS,
                       win32process.ABOVE_NORMAL_PRIORITY_CLASS,
                       win32process.HIGH_PRIORITY_CLASS,
                       win32process.REALTIME_PRIORITY_CLASS]
    if pid == None:
        pid = win32api.GetCurrentProcessId()
    handle = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, True, pid)
    win32process.SetPriorityClass(handle, priorityclasses[priority])

Is there better and more pythonic way?


Solution

  • Your code doesn't do what you say it does. It sets priority for a program.

    DoEvents is an internal VB thing that allows VB to suspend its code and run other code in your program. Thus it's dangerous because events can become reentrant.

    The part about switching to the OS is done at the end of DoEvents and it calls the Windows API to call Sleep(0) which is sleep 0 seconds but surrender the rest of your 20 ms of time to other processes.

    See documentation: https://learn.microsoft.com/en-us/windows/desktop/api/synchapi/nf-synchapi-sleep

    More on DoEvents.

    In VBA/VB6 all functions/subs/properties/methods run from Sub to End Sub. No other sub/function can run while another is running. DoEvents changes this.

    In the distance past of WordBasic etc one used sendkeys to send keystrokes to the application you were running in (Word for Wordbasic). But because your macro was running Word couldn't so didn't update it's state. One used DoEvents with SendKeys to automate things in word that weren't programmable and have it update it's UI. WordBasic didn't have events.

    You can't change global variables, can't use global variables that may change, you can't change input parameters, and you have to make sure the sub/function with DoEvents isn't re-entrant (or only a limited number of times). Or bad things happen and it may not be predictable. EG calling doevents in a selectionchange event that changes the selection will eventually crash with out of stack space.