2

I have a Python application that regularly dies when the computer goes to sleep. It works well on Mac, Windows and Linux on it's own, but the sleep problem is irritating.

Is there a function/library that would let me deal with sleep mode in a platform independant way?

Something like

 if(OSSLEEP){ 
     #Reduced Functionality
 }

I would be happy if the program simply didn't crash, as I realize it is probable that no functionality is possible during sleep.

I researched the question: How can I check to see if system is in standby mode?

It seems there is a Win32 API function which does this: By this reasoning there might be a Python library that does the same thing. Notification when Windows enters in sleep mode

This seems to say it's not possible and a bad idea: Multithreaded python urllib2-based downloader drives the computer to Standby/Sleep

I would love to know if this is do-able even if it's not advisable.

Community
  • 1
  • 1
MrSynAckSter
  • 1,681
  • 1
  • 18
  • 34
  • 5
    What's actually causing it to die on wakeup? Interrupted network connections? You can't do anything while the machine is sleeping, or it wouldn't be sleeping. Catch the actual exception that's killing it. – Wooble Sep 14 '12 at 20:42
  • This is more a case of me modding someone else's app, so I'm not sure if he has a good error catching system. It's trying to talk to USB while it sleeps. I was just curious to see if there was a standard way of dealing with sleep mode in Python. From what I see App just dies "Python.Exe has stopped working" – MrSynAckSter Sep 14 '12 at 20:50
  • 1
    There's always an error catching system in Python. Unless you're using ctypes or custom C extensions (or sending signals to yourself), the app is failing because of an uncaught exception, and you just need to catch/log/rethrow that exception. – abarnert Sep 14 '12 at 21:43
  • Also, it can't possibly die while the computer is asleep; it's either dying while the computer is preparing for sleep, or right after the computer has woken up, and it's probably worth figuring out which. – abarnert Sep 14 '12 at 21:45
  • Read your first linked question (http://stackoverflow.com/questions/387141/how-can-i-check-to-see-if-system-is-in-standby-mode) more carefully: The accepted answer is obliquely telling the poster that the question is confused, because **nothing happens while in standby mode**. As your second link shows, you can at best be notified when the computer is about to enter standby, or after it has resumed running. – alexis Sep 16 '12 at 12:40

1 Answers1

1

During sleep mode, no program is running. That's kind of the point, since modern CPUs use significantly virtually no power when not doing anything.

Additionally, your problem is not exclusive to the sleep mode. It will also occur if the connection drops out, or if your process doesn't get notified during hibernation, or if your network interface is restarted, or … .

So the correct way to solve your problem is: Ask a new question with your code and the error you're getting.

Community
  • 1
  • 1
phihag
  • 278,196
  • 72
  • 453
  • 469