简体   繁体   English

time.sleep 不适用于多线程

[英]time.sleep does not work for multithreaded

I was trying to multithread the code for "Suspend / Hibernate pc with python" provided by Ronan Paixão when I find that time.sleep() does not suspend the thread that run pywin32 module.当我发现time.sleep()没有挂起运行pywin32模块的线程时,我试图对Ronan Paixão提供的“Suspend / Hibernate pc with python”代码进行多线程处理。

>>> Warning! >>> 警告! The following code will put Windows to sleep <<<以下代码将使 Windows 进入睡眠状态 <<<

def suspend(buffer, hibernate=False):
    '''Puts Windows to Suspend/Sleep/Standby or Hibernate.

    Parameters
    ----------
    buffer: string, for time.sleep()
    hibernate: bool, default False
        If False (default), system will enter Suspend/Sleep/Standby state.
        If True, system will Hibernate, but only if Hibernate is enabled in the
        system settings. If it's not, system will Sleep.

    Example:
    --------
    >>> suspend()
    '''
    print('before sleep') 
    sleep(float(buffer))
    print('after sleep')
    # Enable the SeShutdown privilege (which must be present in your
    # token in the first place)
    priv_flags = (win32security.TOKEN_ADJUST_PRIVILEGES |
                  win32security.TOKEN_QUERY)
    hToken = win32security.OpenProcessToken(
        win32api.GetCurrentProcess(),
        priv_flags
    )
    priv_id = win32security.LookupPrivilegeValue(
        None,
        win32security.SE_SHUTDOWN_NAME
    )
    old_privs = win32security.AdjustTokenPrivileges(
        hToken,
        0,
        [(priv_id, win32security.SE_PRIVILEGE_ENABLED)]
    )

    if (win32api.GetPwrCapabilities()['HiberFilePresent'] == False and
            hibernate == True):
        import warnings
        warnings.warn("Hibernate isn't available. Suspending.")
    try:
        windll.powrprof.SetSuspendState(not hibernate, True, False)
    except:
        # True=> Standby; False=> Hibernate
        # https://msdn.microsoft.com/pt-br/library/windows/desktop/aa373206(v=vs.85).aspx
        # says the second parameter has no effect.
        #        ctypes.windll.kernel32.SetSystemPowerState(not hibernate, True)
        win32api.SetSystemPowerState(not hibernate, True)

    # Restore previous privileges
    win32security.AdjustTokenPrivileges(
        hToken,
        0,
        old_privs
    )


if __name__ == '__main__':
    Thread(target=suspend, args=("10")).start()

The print function did waited for time.sleep() , but the Windows was immediately put to sleep. print函数确实等待time.sleep() ,但 Windows 立即进入睡眠状态。 What happened?发生了什么?

You're passing the buffer argument in the call to Thread incorrectly, so it's only sleep ing for 1.0 second.您在对Thread的调用中错误地传递了缓冲区参数,因此它仅sleep 1.0 秒。 You need to add a comma , at the end of value for the args keyword argument like this:您需要在args关键字参数的值末尾添加一个逗号,如下所示:

.
.
if __name__ == '__main__':
    Thread(target=suspend, args=("10",)).start()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM