利用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'在这个等待过程中并不耽误我们干一些其他事情'  

评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!