2013-04-13 23 views
1

我正在使用webapp2進行web開發。在我的開發環境中,我該如何自動重新加載代碼更改?我正在使用httpserver.serve(app, host='127.0.0.1', port='8008'),但每當我更改我的代碼時,我需要停止服務器並重新啓動它。webapp2:自動重新加載代碼更改

我使用webapp2的谷歌提供的谷歌應用程序引擎啓動,我不需要每次我做出改變的時間來重新啓動。他們是如何做到的呢?他們是否監視文件系統更改並在模塊發生更改時致電reload

+1

這是不是一個真正的GAE的問題。您使用webapp2的事實幾乎肯定與您的問題無關。 – allyourcode

+0

按照你的建議我刪除了GAE標籤。 –

+0

經過我的研究,我相信這與GAE用戶有關,所以我將添加標籤和Django標籤。 –

回答

1

原來,谷歌提供的谷歌應用程序引擎啓動從Django框架借用自動重碼,具體如下:https://github.com/django/django/blob/master/django/utils/autoreload.py

搜索#1和有提供解答一些相關的問題: How to automatically reload Django when files change? 這轉鏈接到這個模塊,它能夠檢測並重新加載改變的代碼:https://github.com/tjwalch/django-livereload-server

如果有人想對我是如何做的更多細節。

在我啓動:

import autoreloader,thread 
thread.start_new_thread(autoreloader.reloader_thread,()) 
... 
app = webapp2.WSGIApplication(...) 
def main(): 
    from paste import httpserver 
    httpserver.serve(app, ...) 

autoreloader.py:

import sys,os,time,logging 

RUN_RELOADER = True 

logger = logging.getLogger(__name__) 

whitelist = ['webapp2', 'paste', 'logging'] 

# this code is from autoreloader 
_mtimes = {} 
_win = (sys.platform == "win32") 
_error_files = [] 
_cached_modules = set() 
_cached_filenames = [] 


def gen_filenames(only_new=False): 
    global _cached_modules, _cached_filenames 
    module_values = set(sys.modules.values()) 
    _cached_filenames = clean_files(_cached_filenames) 
    if _cached_modules == module_values: 
     # No changes in module list, short-circuit the function 
     if only_new: 
      return [] 
     else: 
      return _cached_filenames + clean_files(_error_files) 

    new_modules = module_values - _cached_modules 
    new_filenames = clean_files(
     [filename.__file__ for filename in new_modules 
     if hasattr(filename, '__file__')]) 

    _cached_modules = _cached_modules.union(new_modules) 
    _cached_filenames += new_filenames 
    if only_new: 
     return new_filenames + clean_files(_error_files) 
    else: 
     return _cached_filenames + clean_files(_error_files) 

def clean_files(filelist): 
    filenames = [] 
    for filename in filelist: 
     if not filename: 
      continue 
     if filename.endswith(".pyc") or filename.endswith(".pyo"): 
      filename = filename[:-1] 
     if filename.endswith("$py.class"): 
      filename = filename[:-9] + ".py" 
     if os.path.exists(filename): 
      filenames.append(filename) 
    return filenames 

# this code is modified from autoreloader 
def check_code_changed(): 
    global _mtimes, _win 
    for filename in gen_filenames(): 
     stat = os.stat(filename) 
     mtime = stat.st_mtime 
     if _win: 
      mtime -= stat.st_ctime 
     if filename not in _mtimes: 
      _mtimes[filename] = mtime 
      continue 
     if mtime != _mtimes[filename]: 
      _mtimes = {} 
      try: 
       del _error_files[_error_files.index(filename)] 
      except ValueError: 
       pass 
      mname = filename.split('/')[-1].split('.')[0] 
      logger.info('CHANGED %s, RELOADING %s' % (filename,mname)) 
      try: 
       reload(sys.modules[mname]) 
      except: 
       pass 

    return False 

def reloader_thread(): 
    while RUN_RELOADER: 
     check_code_changed() 
     time.sleep(1) 
+0

對我的自動加載器的意見和建議更改歡迎! –

相關問題