Max*_*Dev 9 python linux windows python-3.x sleep-mode
如何在不使用不同操作系统(Ubuntu、Windows ...)上的额外应用程序的情况下防止 python 上的睡眠模式,但在大多数情况下我需要 Linux 解决方案
我正在制作可以长时间工作的应用程序。它使用了大约 80% 的 CPU,因此用户只需启动此应用程序即可离开键盘。所以我想我需要一些类似系统 api 或库来锁定睡眠模式的东西。我确信,它存在。例如,如果您在操作系统上打开任何视频播放器,您的(PC、笔记本电脑)将不会进入睡眠模式,在浏览器中也是如此。
此外,在 Android ( WakeLock ) 或 Windows (SetThreadExecutionState) 中也有同样的事情
np8*_*np8 15
在谷歌搜索寻找解决方案时,没有可用的包,所以我决定将其打包以将其放入 PyPI:wakepy。我获得了跨平台支持的 PR,目前wakepy 支持 Windows、Linux 和 macOS。
python -m wakepy [-p]
Run Code Online (Sandbox Code Playgroud)
使用可选-p
标志将使用演示模式,该模式使屏幕保持打开和解锁状态。
防止睡眠:
python -m wakepy [-p]
Run Code Online (Sandbox Code Playgroud)
防止屏幕保护程序/屏幕锁定(和睡眠):
from wakepy import keep
with keep.running():
# do stuff that takes long time
Run Code Online (Sandbox Code Playgroud)
创建示例 TK 应用程序以保持 Windows 唤醒状态
import tkinter as tk
import ctypes
import sys
def display_on():
global root
print("Always On")
ctypes.windll.kernel32.SetThreadExecutionState(0x80000002)
root.iconify()
def display_reset():
ctypes.windll.kernel32.SetThreadExecutionState(0x80000000)
sys.exit(0)
root = tk.Tk()
root.geometry("200x60")
root.title("Display App")
frame = tk.Frame(root)
frame.pack()
button = tk.Button(frame,
text="Quit",
fg="red",
command=display_reset)
button.pack(side=tk.LEFT)
slogan = tk.Button(frame,
text="Always ON",
command=display_on)
slogan.pack(side=tk.LEFT)
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
我遇到了类似的情况,其中一个进程需要足够长的时间来执行自己,Windows 会休眠。为了克服这个问题,我写了一个脚本。
下面一段简单的代码可以防止这个问题。使用时,它会要求 Windows 在脚本运行时不要休眠。(在某些情况下,例如电池电量耗尽时,Windows 会忽略您的请求。)
class WindowsInhibitor:
'''Prevent OS sleep/hibernate in windows; code from:
https://github.com/h3llrais3r/Deluge-PreventSuspendPlus/blob/master/preventsuspendplus/core.py
API documentation:
https://msdn.microsoft.com/en-us/library/windows/desktop/aa373208(v=vs.85).aspx'''
ES_CONTINUOUS = 0x80000000
ES_SYSTEM_REQUIRED = 0x00000001
def __init__(self):
pass
def inhibit(self):
import ctypes
print("Preventing Windows from going to sleep")
ctypes.windll.kernel32.SetThreadExecutionState(
WindowsInhibitor.ES_CONTINUOUS | \
WindowsInhibitor.ES_SYSTEM_REQUIRED)
def uninhibit(self):
import ctypes
print("Allowing Windows to go to sleep")
ctypes.windll.kernel32.SetThreadExecutionState(
WindowsInhibitor.ES_CONTINUOUS)
Run Code Online (Sandbox Code Playgroud)
要运行脚本,只需:
import os
osSleep = None
# in Windows, prevent the OS from sleeping while we run
if os.name == 'nt':
osSleep = WindowsInhibitor()
osSleep.inhibit()
# do slow stuff
if osSleep:
osSleep.uninhibit()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
12516 次 |
最近记录: |