I have a python script that is downloading data on a remote computer. I want to prevent the computer from going to sleep as long as the data download is in progress. However if the program finishes/hangs/crashes, I want the computer to go to sleep.

I do not want to disable hibernation settings on my computer. I've looked around at programs like insomnia & command line shortcuts ( How can I put the computer to sleep from Command Prompt/Run menu? ) but they aren't perfect for the job. Any way I can put a try:except:finally block around my python code and call some function to put the computer to sleep ?

Update : Found this answer useful : What is the command to use to put your computer to sleep (not hibernate)? . I created a little one line sleep app that I can call from my python finally block using os.system call.

  • Usually, there is always network traffic, especially if you run Windows. Sep 11 '16 at 9:20

One possible way to avoid this problem is to make use of your network adapter power management options.

  1. Type Device Manager in your Start Menu search box and run the application.
  2. Locate and Right-Click your network adapter
  3. Move to the Power Management tab.
  4. Either:
    1. Turn off "Allow the computer to turn off this device to save power", or
    2. Turn off "Only allow a magic packet to wake the computer".

I'd probably suggest 4.2 first and see if it works with your network adapter. This will keep power management features activated for your network adapter. 4.1 will remove them entirely.

Alternatively, you can try and keep magic packets on and use them from within python to keep the computer awake: Start here: Power Management for Network Devices in Windows 7. You are interested in the comments at the end of the article.

If you configure the Windows 7 network adaptor to "Only Allow Magic Packets to wake up the computer" you can send Magic Packets using the free WolCmd.exe command line utility. Since this method was not not 100% reliable I created a batch file that sends the command 4 times as follows using 4 different inbound UDP ports forwarded to the LAN ip address of the Thinkpad. Using 4 different ports was an attempt to improve 'wake-up' and delivery reliability. Sending the command 4 times definitely helped reliabilty. I don't know if using 4 different UDP ports through the firewall made any difference.

Wolcmd is available here.

  • 1
    +1 4.2 would work, but WOL is an ugly scenario, network traffic shouldn't be interrupted in this case
    – user8228
    Jul 13 '11 at 13:41
  • Yup. I agree the WOL alternative would be ugly. One could conceivably hide the nastiness by filtering the external command through a command prompt hiding script like discussed here: superuser.com/questions/309243/…. But nonethless there's something always inherently fishy about calling external commands from within source code :)
    – A Dwarf
    Jul 13 '11 at 14:05
  • I don't see option 4.2 on my wireless card. What does 4.1 imply ? That keep the wireless card on even if the computer is put to sleep ? How would that help my case i.e. keep the script running..
    – user
    Jul 13 '11 at 14:52
  • 4.1 disables power management to the card. If your computer goes to sleep the card will keep working. This means of course, that there wil be no power savings from this card. But your script will continue to operate.
    – A Dwarf
    Jul 13 '11 at 14:56
  • Can you point me to an article that explains this. I thought sleep means the processor goes into a low power state. Where would my script run ? If the wireless card is on, how would my computer sleep when the script is done?
    – user
    Jul 13 '11 at 15:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.