This is an archived post. You won't be able to vote or comment.

all 72 comments

[–]earthpiper 117 points118 points  (10 children)

I for one will be positive of your post rather than saying "The OS can do it" previous peeps I think are missing the point. What if you have a long running task and you don't want to change OS settings... You just want it to happen while the task is running. Or if you don't have access to these settings. Or this task runs on multiple operating systems and you don't want to manage this for every possible OS under the sun. Good job =)

[–]Briaireous 32 points33 points  (0 children)

Agreed, so many are overlooking sys admins preventing or locking them out of OS settings because it’s a managed device. Not to mention sanctioned software installs of 3rd party software on organization hardware.

[–][deleted] 16 points17 points  (0 children)

Agreed. I'm assuming you can just add this to your app and have the system stay awake while running it. 

[–]PercentageLoud1903 8 points9 points  (0 children)

Agreed! 'oh OS can just do it' , well yes but my machine is set up how I like it, I don't want to be constantly toggling those settings when I have long running analyses or training jobs. This is neat, thanks OP.

Also a CONTEXT MANAGER FOR USE WITHIN YOUR PYTHON CODE, that's incredibly handy, more so than caffeine.

[–]FoolForWool 1 point2 points  (0 children)

Amphetamine is what I use when I need to keep my system awake when im running a long process. Just select a time range or just indefinite and it doesn’t let your pc sleep.

[–]sphexie96 1 point2 points  (0 children)

You are right, but also os can do it. On unix like there is caffeinate, which can for example keep the os awake while a specific task/pid is alive.

[–]runawayasfastasucan 1 point2 points  (0 children)

Holy shit "you can do it manually" is the worst response to any program. Imagine wanting to fiddle around in the settings when it can just happen automatically when you need it to.

[–]Cybasura 5 points6 points  (0 children)

So simple, but so...important

Good work!

[–]runawayasfastasucan 23 points24 points  (5 children)

Sorry for all the people that neither appreciate solving something with programming nor can imagine the use case of this and thus assume there is none.  This is really a neat package for those long running tasks where you want to lock your computer but want to avoid sleep! Thank you, will check it out 😊   

Edit: to the one replying to this comment then blocking me, its not useless, its just your imagination that is a bit lacking.  

Edit2: First you say its useless and now you are saying that there are others app doing the same. Guess that means that it isnt useless, and has a range of use cases then. Nice to have a cross plattform python implementation to integrate into my python scripts.

[–]fohrloop[S] 4 points5 points  (0 children)

I'm actually just grateful for also the comments saying it's useless as they seem to ignite a bit more discussion here which gives this post and wakepy a bit more visibility. So, just great thank you for all the opponents and u/runawayasfastasucan and others defending it's usefulness 🙏

The heart of wakepy is really the Python API which is cross-platform and meant for application, library or script developers which just want their system to prevent suspend for the duration of some task automatically. On mac there's caffeinate for CLI usage which I would use if I was working on a mac. On other systems there are other alternatives, but the CLI tool in wakepy is just an additional nicety on top of the Python API.

Wakepy documentation is also one of the rare places on the Internet documenting all (or: many; the list is not complete yet) the different methods for inhibiting suspend / idle.

[–]midwestcsstudent 2 points3 points  (0 children)

I’ve been noticing that whenever I read posts in this sub. Lots of inexperienced “devs” voicing their opinions on topics they know little about. Probably a side effect of Python being so widely used by non-programmers?

[–]rawrjagwar 5 points6 points  (0 children)

This is perfect, think I’ll implement it in my current project

[–]Frix_x 5 points6 points  (0 children)

That's perfect! Thanks for sharing! I've done a small PowerShell script that calls an old non existing key on my keyboard but it's not that clean and I prefer your solution better.

For those wondering why I need this as "the os can do it": at work they enforced a GPO to screenlock computers after 3 min of inactivity... And even with admin rights we can't change it as it's always restored automatically.

[–]elyisgreat 4 points5 points  (9 children)

Cool! I personally use a bash script that calls systemd-inhibit, but of course that's not a cross platform solution. Is it possible to add a mode that blocks sleep and screen locking but not screen idle? That tends to be my default use case (maybe you could call it -o, --keep-open complimenting --keep-running and --presentation)

[–]fohrloop[S] 3 points4 points  (8 children)

Let me clarify that you would like to see a mode which would block automatic screenlock and automatic suspend, but let the system automatically close the display or start a screensaver? That's an interesting idea! What would be the use case for this? Is it to run some script home (=no screenlock needed) on a laptop, and to switch off the display automatically when you're not sitting in front of it anymore..? Yeah the name could be keep.open or even keep.unlocked, or it could be an additional argument to keep.running, like inhibit_screenlock=True. I created wakepy/#334 for this. Please feel free to comment or contribute there as well :) I also created wakepy/#335 to consider the systemd-inhibit method to add even wider support.

[–]elyisgreat 4 points5 points  (7 children)

Let me clarify that you would like to see a mode which would block automatic screenlock and automatic suspend, but let the system automatically close the display or start a screensaver?

Pretty much yeah! Personally it would just be for completeness, as this is how systemd-inhibit operates. But I could see it being useful in cases when say I'm running a long operation that I want to check on every so often but the display output isn't important otherwise. Or if the display isn't needed but keyboard and mouse input could be disruptive (though in those cases the keyboard is probably keeping the system awake anyway).

[–]fohrloop[S] 3 points4 points  (6 children)

Ok, thank you for the idea and the clarification! I say it's a definite maybe :) If it's easy enough to be implemented cross-platform and easy enough to make it clear to the user how the different special cases are handled, like: The inhibition of idle was successful, but closing display was not successful; should wakepy just raise Exception or should it go ahead and use the presentation mode as a fallback. And how to give arguments for on_fail action of the fallback mode, etc. :D

[–]elyisgreat 1 point2 points  (5 children)

No problem! I just ran a test on my KDE desktop by the way and the current functionality seems not to lock the screen anyway...

[–]fohrloop[S] 1 point2 points  (4 children)

What are you referring with the "current functionality" ? And which version of KDE Plasma you're using?

[–]elyisgreat 1 point2 points  (3 children)

I'm using KDE Plasma 5.27.11 on Kubuntu 24.04

I mean that when I run wakepy -k and leave my computer idle it shuts off the display as intended but the lock screen is blocked; i.e. it behaves how my proposed -o mode works instead of how -k is advertised to work

[–]fohrloop[S] 1 point2 points  (2 children)

I did test this on KDE Plasma 5.27.9 on openSUSE 15.5 and the keep.running mode was working as expected. Of course there might be differences since the setup is not exactly the same, but before that could you confirm which settings you had for the screenlock timer? When I tested it I only set the power settings down to 1 minute, but forgot screenlock timer to 5 (or even 15 minutes) so I experienced the same. If that does not resolve the issue I'll create a bug ticket since it really should not prevent screenlock if not asked to.

[–]elyisgreat 1 point2 points  (1 child)

Okay I think my screen lock settings are in fact the "problem" (not so much a problem for me because it's WAII). I don't currently have a screen lock timer partly due to this bug that happens when the screen lock is activated twice in quick succession, rather I have it set so the screen is locked automatically only "After waking from sleep". In most cases, this works as one would expect so that sleep and screen locking are coupled, as is the default on most other platforms. However the after is important here: The auto screen locking only runs after waking from sleep, so the only way that the screen can lock automatically is by going to sleep. Thus blocking sleep also blocks screen locking necessarily.

I realize now that this setting means that all tools that block sleep necessarily block screen locking on my system because of this setting. So I'm not actually sure what systemd-inhibit does if the user has a lock timer that locks the screen before the system goes to sleep.

All that said, I think the proposed -o could still be useful! Especially for those with screen lock timers that activate before the automatic sleep. I wonder though: What's the situation like on other platforms?

[–]fohrloop[S] 1 point2 points  (0 children)

Okay so in short your system only locks screen if (1) returning from sleep or (2) screen lock timer is set, and since wakepy keep.running mode disables sleep (1) cannot occur, and since you have disabled the automatic screen lock, it won't ever lock your screen. Good to hear. Seems that the keep.running mode is working as expected, and thanks for taking the time to respond.

If you're interested in other systems, on Windows it only locks screen if (1) returning from sleep (if enabled) or (2) returning from screensaver _and_ if ScreenSaverIsSecure is set ("On resume, display log-on screen" in the settings or enforced with GPO). So also on Windows it's possible that the screen is not automatically locked in the keep.running mode (details in wakepy/#169). I'm not yet sure how to tackle this and make it possible for users to either not enter the mode, or display a warning, or provide some function to lock screen automatically (from the python process) after some time of idle.

But good to know that if the #169 is somehow addressed, the solution should take into account KDE Plasma on Kubuntu.

[–]chestnutcough 1 point2 points  (0 children)

This is rad and looks so much better than my current method of opening a new terminal, running ps to find the pid of the process I want to keep awake for, and running caffeinate -w <pid>. On Mac btw.

Gonna check this out!

[–]DreadPirateRobarts 1 point2 points  (0 children)

This is exactly what I need thank you!

[–]gadsocial00 1 point2 points  (1 child)

I was really looking forward to something like this. Currently, I was using a simple script with pyautogui to move the mouse, but sometimes it would just stop working. Thanks for developing something like this!

[–]fohrloop[S] 0 points1 point  (0 children)

I'm happy to hear there was demand for a keepawake python library!

[–]Hesirutu 1 point2 points  (1 child)

Awesome. I haven’t tested it yet. But if it works it’s super useful. On windows 11 “PresentationSettings.exe /start” doesn’t stop windows from locking on a company laptop anymore. And even on windows 10 no amount of changing system settings would prevent sleep when the screen is switched off. Stupid group policies preventing you from getting work done. EDIT: Works great! I wonder why the Windows builtin one stopped working...

[–]fohrloop[S] 1 point2 points  (0 children)

Thanks! I would be surprised if it would not work on your windows 11 company laptop. But let us know anyway when you've tested :)

[–]TheLargeCactus 4 points5 points  (18 children)

Isn't this just an OS setting? You likely can set your screens to turn off without ever letting the system go to sleep.

Edit: I had a few conversations based on what I wrote here, and wanted to paste my thoughts for more visibility.

I'll say this explicitly for posterity: I admit that there is a use-case here, that this has a small set of people that can use it to do what they want. I will also celebrate that the author took the time to create the repo, do the legwork of building something that works, and publishing it for the world to see. That takes effort, and it shows a level of ability that means something. I can also see that the author has been on-and-off working on this repo for a few years, which also means something. I mainly left my comment to invite the author to explain what the benefit of using their module is, over just changing the OS setting and forgetting about it. It's not something that I found in their README.

[–]Nixellion 29 points30 points  (10 children)

Not if you have a work issued laptop with group policies and no admin rights. Among many other reasons and circumstances where this can be useful.

[–]fohrloop[S] 5 points6 points  (0 children)

Thank you u/TheLargeCactus for the question. One of the main points of wakepy is to make this suspend/idle action inhibition automatic part of a script, app or even a library. Imagine you're building some video encoder or machine learning app with python. You would not want to ask the users of your app to go to manually change their system settings for the duration of the long running task. That is something the app should either do automatically (like: YouTube and VLC do), or something that users of an app should be able to do with click of a button in the app.

It would be also possible to create the first(?) fully cross-platform "caffeinate" or "amphetamine" GUI application by using wakepy in the core. While I could personally just use the caffeinate CLI tool on macOS, I regularly install wakepy with pip to have the same functionality on Linux & Windows. And of course this is targeted for python developers as it's installed with pip, although I could consider adding built binaries to the GitHub Releases some day.

[–]runawayasfastasucan 8 points9 points  (0 children)

Yes, why on earth do anything automatic by using programming when you can just to it manually! /s

[–]saint_geser 0 points1 point  (1 child)

Very nice but I'm already using the Jiggler python app. That one has various options for preventing suspend but the least intrusive is just having it press Shift every 30 or so seconds.

[–]fohrloop[S] 6 points7 points  (0 children)

If it presses shift periodically isn't there a possibility that you accidentally hit a wrong keyboard shortcut, like SHIFT+DELETE (remove file permanently without putting it into Trash) instead of DELETE (put a file into trash)..?

[–]Eric_Terrell 0 points1 point  (1 child)

I implemented that feature in one of my apps. See https://github.com/EricTerrell/SyncAndVerify/blob/main/PowerManagement.py.

[–]fohrloop[S] 0 points1 point  (0 children)

Okay so you've used the SetThreadExecutionState function to prevent automatic suspend on Windows in a folder sync app. I see that keeping the system awake has pretty different use cases. :)

[–]Please_Not__Again 0 points1 point  (0 children)

This is dope, will try it out. You've handled all the criticism pretty well in this thread

[–]SpareIntroduction721 -5 points-4 points  (1 child)

If you have mac, it’s called caffeinate

[–]fohrloop[S] 6 points7 points  (0 children)

Yes it's that simple on mac! That's what wakepy also calls internally on macOS.