Wake Windows PC from sleep in Python 2.7

浪尽此生 提交于 2020-08-20 05:38:01

问题


I have a script that will put the system to sleep in the middle of it. Is there any way to make that script wake the system up and then continue running?

I have read many round-about ways of doing so by Wake on LAN or using Task Scheduler. I am looking for something that would wake it up after a set period of time or after a specific piece of my script is finished. I will need to this to work for Windows 7, 8.1, and 10.

Anyone know of a way to wake from sleep while still running a script?


回答1:


It depends on how you're putting the system to sleep. If using SetSuspendState (with the last argument being FALSE to allow wake-up events), then you can schedule a future wake-up using CreateWaitableTimer followed by SetWaitableTimer.

The respective Python implementation is quite easy using ctypes or, if you want something more pythonic, pywin32.

Hope it helps!




回答2:


I was unable to accomplish this using just python. However in the Windows SDK they provide a tool called pwrtest that will allow you to do timed sleep cycles. I am able to call this with python and then my script continues when pwrtest wakes the PC up from sleep.



来源:https://stackoverflow.com/questions/32705626/wake-windows-pc-from-sleep-in-python-2-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!