如何在django中设置页面加载时间

时间:2014-12-09 09:42:54

标签: python django

我试图为Django设置页面加载时间,我遇到了两种方法,然后出现问题 在模型中我有使用外部模块功能的功能,有时模块功能计算时间可能需要1分钟以上,所以我想限制时间

  1. 使用python信号但是信号命令不会运行,除非线程是主线程
  2. 阅读Django文档后,我尝试在settings.py中使用TIME_OUT变量,但它没有影响任何内容
  3. Views.py:

    from django.shortcuts import render_to_response
    from web.models import SomeClass
    instance = SomeClass()
    
    def get_service(request):
        service_instance.some_func()
        return render_to_response('get.html', dic, context_instance=RequestContext(request))
    

    models.py

    from x import y
    temp = y()
    class SomeClass():
        def some_func(self):
            temp.do_somthing()
    

    settings.py:

    """
    Django settings for webservice project.
    
    For more information on this file, see
    https://docs.djangoproject.com/en/1.6/topics/settings/
    
    For the full list of settings and their values, see
    https://docs.djangoproject.com/en/1.6/ref/settings/
    """
    
    # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
    import os
    BASE_DIR = os.path.dirname(os.path.dirname(__file__))
    
    
    # Quick-start development settings - unsuitable for production
    # See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/
    
    # SECURITY WARNING: keep the secret key used in production secret!
    SECRET_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX'
    
    # SECURITY WARNING: don't run with debug turned on in production!
    DEBUG = True
    #DEBUG = False
    TEMPLATE_DEBUG = DEBUG
    TIME_OUT = 2 
    ALLOWED_HOSTS = ['*']
    
    SETTINGS_PATH = os.path.normpath(os.path.dirname(__file__))
    
    TEMPLATE_DIRS = (
        os.path.join(SETTINGS_PATH, 'templates'),
    )
    
    # List of callables that know how to import templates from various sources.
    TEMPLATE_LOADERS = (
        'django.template.loaders.filesystem.Loader',
        'django.template.loaders.app_directories.Loader',
    
    #     'django.template.loaders.eggs.Loader',
    )
    # Application definition
    INSTALLED_APPS = (
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
    )
    
    MIDDLEWARE_CLASSES = (
        'django.contrib.sessions.middleware.SessionMiddleware',
        'django.middleware.common.CommonMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
        'django.middleware.clickjacking.XFrameOptionsMiddleware',
    )
    
    ROOT_URLCONF = 'webservice.urls'
    
    WSGI_APPLICATION = 'webservice.wsgi.application'
    
    
    # Database
    # https://docs.djangoproject.com/en/1.6/ref/settings/#databases
    
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        }
    }
    
    # Internationalization
    # https://docs.djangoproject.com/en/1.6/topics/i18n/
    
    LANGUAGE_CODE = 'en-us'
    
    TIME_ZONE = 'UTC'
    
    USE_I18N = True
    
    USE_L10N = True
    
    USE_TZ = True
    
    # Static files (CSS, JavaScript, Images)
    # https://docs.djangoproject.com/en/1.6/howto/static-files/
    
    STATIC_URL = '/static/'
    

    我想为some_func函数构建超时或更好地限制django设置中的页面加载时间 主要问题是some_func调用另一个模块,所以我不能在time.time时使用循环,因为之后 如果有人知道解决这个问题的想法或方法,我将非常感激

    我正在使用python 2.7和Django 1.6

0 个答案:

没有答案