Python利用多線程定時執行cmd命令關機

cdfd 9年前發布 | 6K 次閱讀 Python

利用os模塊可以執行cmd命令,利用這一點可以實現定時關機,然而在等待關機的過程中也不能啥都不干,于是多線程派上了用場。

    #! /usr/bin/env python

#coding=utf-8  
#這里需要引入三個模塊  
import time, os, sched, easygui, thread  

# 第一個參數確定任務的時間,返回從某個特定的時間到現在經歷的秒數   
# 第二個參數以某種人為的方式衡量時間   
schedule = sched.scheduler(time.time, time.sleep)   

def perform_command(cmd, inc):  
    os.system(cmd)  
def reminder(cmd, inc = 60):  
    # enter用來安排某事件的發生時間,從現在起第n秒開始啟動  
    schedule.enter(inc, 0, perform_command, (cmd, inc))  
    # 持續運行,直到計劃時間隊列變成空為止   
    schedule.run()   
    thread.exit_thread()      
if __name__=='__main__':      
    print("show time after 10 seconds:")   
    thread.start_new_thread(reminder, ("shutdown -s", 10))  
    print'在這個等待過程中并不耽誤我們干一些其他事情'  </pre> 


 本文由用戶 cdfd 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!