玩蛇网提供最新Python编程技术信息以及Python资源下载!
您现在的位置: 玩蛇网首页 > Python问题解答 > 正文内容

Python自动运行脚本求解

我现在执行一个python脚本。例如 print 123。 然后运行。
在运行的过程中,我将 print 123 修改成 print 3333.
怎么样让这个python自动运行。 求解。

tornado也有这样地机制,具体可以看tornado源码,使用上比如:

    config_evn = settings.get("config").split(".")[-1].lower()
    if settings.get("debug") or config_evn == "debug":
        # 检测py文件的变动, 自动reload新的代码, 无需重启服务器.
        # 仅用于开发和测试阶段, 否则每次修改都重启Tornado服务.
        instance = tornado.ioloop.IOLoop.instance()
        tornado.autoreload.start(instance)
        instance.start()
    else:
        tornado.ioloop.IOLoop.instance().start()

比较合适和常见的处理方法,是另外做一个daemon,定时去轮询文件,比如检查文件的修改时间,md5值等,一旦发现文件被修改,就重新用subprocess模块调用下这个文件,大多数python做热部署的代码都是这么搞的。
举个例子
来自https://github.com/jimjose/pyreload/blob/master/pyreload.py
flask里面也有类似的file reloader机制 可以参考 http://stackoverflow.com/questions/16344756/auto-reloading-python-flask-app-upon-code-changes

"""
A file monitor and module reloader.
Major part of the code is taken directly from the original "paste.reloader" module. I just created this stripped down version for one of my projects and wanted to share if anyone else is interested.
**all credits goes to original authors @ Paste **
http://pythonpaste.org/modules/reloader.html
Use this like:
..code-block:: Python
    import pyreload
    pyreload.install(verbose=True, poll_interval=1)
Now this will watch for any changes in the files and will reload the corresponding module automatically, so there is no need to restart the python server.
"""

import os
import imp
import sys
import time
import threading

def install(verbose=True, poll_interval=1):
    """
    Install the reloading monitor.
    """
    mon = Monitor(verbose=verbose,poll_interval=poll_interval)
    t = threading.Thread(target=mon.periodic_reload)
    t.setDaemon(True)
    t.start()

class Monitor(object):
    def __init__(self, verbose, poll_interval):
        self.verbose = verbose;
        self.module_mtimes = {}
        self.poll_interval = poll_interval

    def periodic_reload(self):
        while True:
            self.check_reload()
            time.sleep(self.poll_interval)

    def check_reload(self):
        filenames = []
        for module in sys.modules.values():
            try:
                filename = module.__file__
            except (AttributeError, ImportError), exc:
                continue

            try:
                stat = os.stat(filename)
                if stat:
                    mtime = stat.st_mtime
                else:
                    mtime = 0
            except (OSError, IOError):
                continue

            if filename.endswith('.pyc') and os.path.exists(filename[:-1]):
                mtime = max(os.stat(filename[:-1]).st_mtime, mtime)
            elif filename.endswith('$py.class') and \
                    os.path.exists(filename[:-9] + '.py'):
                mtime = max(os.stat(filename[:-9] + '.py').st_mtime, mtime)
            if not self.module_mtimes.has_key(filename):
                self.module_mtimes[filename] = mtime
            elif self.module_mtimes[filename] < mtime:
                if self.verbose:
                    print >> sys.stderr, (
                        "pyreload: %s changed; reloading..." % filename)
                try:
                    imp.reload( module )
                    if self.verbose:
                        print >> sys.stderr, ("pyreload: reloaded: %s" % module.__name__ )
                except Exception as e:
                    if self.verbose:
                        print >> sys.stderr, ("pyreload: reloaded: %s" % e.message )
                    pass

                del self.module_mtimes[filename]

玩蛇网文章,转载请注明出处和文章网址:http://www.iplaypy.com/wenda/wd18974.html [复制]



我要小额赞助,鼓励作者写出更好的教程↓↓↓

玩蛇网Python QQ群,欢迎加入: ① 279974227 玩蛇网Python新手群
修订日期:2017年05月22日 - 11时48分48秒 发布自玩蛇网

我要分享到:
评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)

必知PYTHON教程 Must Know PYTHON Tutorials

必知PYTHON模块 Must Know PYTHON Modules