Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 in Windows Vista? ) 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.

share|improve this question

1 Answer 1

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.

share|improve this answer
1  
+1 4.2 would work, but WOL is an ugly scenario, network traffic shouldn't be interrupted in this case –  Revolter 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.. –  buffer 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? –  buffer Jul 13 '11 at 15:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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