python 脚本自动重载

系统 172 0

利用flask自带的werkzeug模块实现

            
              # 监控脚本
# 首先需要 pip install flask
# pymonitor
              
                .
              
              py
def 
              
                run_with_reloader
              
              
                (
              
              main_func
              
                ,
              
               args
              
                =
              
              
                (
              
              
                )
              
              
                ,
              
               kwargs
              
                =
              
              None
              
                ,
              
              
                      extra_files
              
                =
              
              None
              
                ,
              
               interval
              
                =
              
              
                1
              
              
                ,
              
              
                      reloader_type
              
                =
              
              
                'auto'
              
              
                )
              
              
                :
              
              
                ""
              
              
                "Run the given function in an independent python interpreter."
              
              
                ""
              
              
                import
              
               os
    
              
                import
              
               sys
    
              
                import
              
               signal
    
              
                import
              
               threading
    
              
                from
              
               werkzeug
              
                .
              
              _reloader 
              
                import
              
               reloader_loops
    reloader 
              
                =
              
               reloader_loops
              
                [
              
              reloader_type
              
                ]
              
              
                (
              
              extra_files
              
                ,
              
               interval
              
                )
              
              
    signal
              
                .
              
              
                signal
              
              
                (
              
              signal
              
                .
              
              
                SIGTERM
              
              
                ,
              
               lambda 
              
                *
              
              args
              
                :
              
               sys
              
                .
              
              
                exit
              
              
                (
              
              
                0
              
              
                )
              
              
                )
              
              
                try
              
              
                :
              
              
                if
              
               os
              
                .
              
              environ
              
                .
              
              
                get
              
              
                (
              
              
                'WERKZEUG_RUN_MAIN'
              
              
                )
              
              
                ==
              
              
                'true'
              
              
                :
              
              
            t 
              
                =
              
               threading
              
                .
              
              
                Thread
              
              
                (
              
              target
              
                =
              
              main_func
              
                ,
              
               args
              
                =
              
              args
              
                ,
              
               kwargs
              
                =
              
              kwargs
              
                )
              
              
            t
              
                .
              
              
                setDaemon
              
              
                (
              
              True
              
                )
              
              
            t
              
                .
              
              
                start
              
              
                (
              
              
                )
              
              
            reloader
              
                .
              
              
                run
              
              
                (
              
              
                )
              
              
                else
              
              
                :
              
              
            sys
              
                .
              
              
                exit
              
              
                (
              
              reloader
              
                .
              
              
                restart_with_reloader
              
              
                (
              
              
                )
              
              
                )
              
              
    except KeyboardInterrupt
              
                :
              
              
        pass


            
          
            
              # 运行脚本

              
                import
              
               time


              
                from
              
               pymonitor 
              
                import
              
               run_with_reloader


def 
              
                main
              
              
                (
              
              name
              
                ,
              
               age
              
                )
              
              
                :
              
              
                while
              
               True
              
                :
              
              
                print
              
              
                (
              
              
                "hello, "
              
              
                ,
              
               name
              
                ,
              
              
                '!'
              
              
                )
              
              
        time
              
                .
              
              
                sleep
              
              
                (
              
              
                2
              
              
                )
              
              
                print
              
              
                (
              
              age
              
                )
              
              
                if
              
               __name__ 
              
                ==
              
              
                '__main__'
              
              
                :
              
              
                run_with_reloader
              
              
                (
              
              main
              
                ,
              
               args
              
                =
              
              
                (
              
              
                'man'
              
              
                ,
              
              
                20
              
              
                )
              
              
                )
              
            
          

参考链接: https://fuyunliu.github.io/2018/03/09/python-reloader/


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请请扫描上面二维码支持博主1元、2元、5元等您想捐的金额吧,狠狠点击下面给点支持吧

发表我的评论
最新评论 总共0条评论