2013-04-13 3 views
1

웹 개발을 위해 webapp2를 사용하고 있습니다. 내 dev 환경에서 어떻게 코드를 자동으로 다시로드 할 수 있습니까? httpserver.serve(app, host='127.0.0.1', port='8008')을 사용하고 있지만 코드를 변경할 때마다 서버를 중지하고 다시 시작해야합니다.webapp2 : 자동으로 코드 변경 재로드

Google에서 제공하는 Google App Engine 실행기와 함께 webapp2를 사용했으며 변경을 할 때마다 다시 시작할 필요가 없습니다. 그들은 그걸 어떻게 햇어? 파일 시스템 변경 사항을 모니터링하고 모듈이 변경되면 reload으로 전화합니까? https://github.com/django/django/blob/master/django/utils/autoreload.py

유래를 검색 및 답변 제공 관련된 몇 가지 질문이 있었다 :

+1

이 정말 GAE 질문이 아니다. 당신이 webapp2를 사용하고 있다는 사실은 거의 당신의 문제와 관련이 없습니다. – allyourcode

+0

제안에 따라 GAE 태그를 제거했습니다. –

+0

내 연구 후 이것이 GAE 사용자와 관련이 있다고 생각하므로 장고 태그와 함께 태그를 추가 할 것입니다. –

답변

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

내 오토로더 변경에 대한 의견 및 제안을 환영합니다! –