1

My system has following version of software installed:

I have created two environment using virtualenv for two Django projects(just default It worked! page) one hosted on one.local.com(VirtualHost) and other on two.local.com(VirtualHost). Below code is the VirtualHost configuration for Apache's httpd-vhost.conf file.

<VirtualHost *:80>
  WSGIApplicationGroup %{ENV:ONE_GROUP}
  ServerName one.local.com
  ServerAdmin admin@example.com
  ErrorLog "D:/_pythonDev/Projects/logs/one.local.com-error.log"
  CustomLog "D:/_pythonDev/Projects/logs/one.local.com-access.log" common

  WSGIScriptAlias / "D:/_pythonDev/Projects/Project1/Project1/wsgi.py" application-group=%{ENV:ONE_GROUP}
  # I also tried WSGIImportScript
  <Directory "D:/_pythonDev/Projects/Project1/Project1">
    <Files wsgi.py>
      Require all granted
    </Files>
  </Directory>

  Alias /favicon.ico "D:/_pythonDev/Projects/Project1/static/favicon.ico"
  Alias /static/ "D:/_pythonDev/Projects/Project1/static/"
  <Directory "D:/_pythonDev/Projects/Project1/static">
    Require all granted
  </Directory>
</VirtualHost>

<VirtualHost *:80>
  WSGIApplicationGroup %{ENV:TWO_GROUP}
  ServerName two.local.com
  ServerAdmin admin@example.com
  ErrorLog "D:/_pythonDev/Projects/logs/two.local.com-error.log"
  CustomLog "D:/_pythonDev/Projects/logs/two.local.com-access.log" common

  WSGIScriptAlias / "D:/_pythonDev/Projects/Project2/Project2/wsgi.py" application-group=%{ENV:TWO_GROUP}

  <Directory "D:/_pythonDev/Projects/Project2/Project2">
    <Files wsgi.py>
      Require all granted
    </Files>
  </Directory>

  Alias /favicon.ico "D:/_pythonDev/Projects/Project2/static/favicon.ico"
  Alias /static/ "D:/_pythonDev/Projects/Project2/static/"
  <Directory "D:/_pythonDev/Projects/Project2/static">
    Require all granted
  </Directory>
</VirtualHost>

Following is the wsgi.py for one.local.com

import os
import sys
import site

site.addsitedir("D:/_pythonDev/env/env1/Lib/site-packages")

sys.path.append("D:/_pythonDev/Projects/Project1/Project1")
sys.path.append("D:/_pythonDev/Projects/Project1")

activate_env_file = "D:/_pythonDev/env/env1/Scripts/activate_this.py"

exec(open(activate_env_file).read(), dict(__file__=activate_env_file))

from django.core.wsgi import get_wsgi_application

os.environ["DJANGO_SETTINGS_MODULE"] = "Project1.settings"

application = get_wsgi_application()

Problem:

  • Loading time is unstable it takes about 10s.

Answer required:

  • Reason of unstable loading time.
  • Answer to solve it.
Balakrishnan
  • 2,403
  • 2
  • 26
  • 47
  • You should try using a profiler first to see where the time is taken, either use the developer tools of your browser (usually F12 then use the "Network" tab) or use django-debug-toolbar – Sayse Jul 28 '15 at 06:39
  • A profiler is probably overkill and may be complicated to setup. Simply import the time module at the start of the WSGI script file and then add 'print time.time()' before and after the call to 'get_wsgi_application()'. This would at least confirm that occurs in Django initialisation. The output would be in seconds. Compare the time at the two places. – Graham Dumpleton Jul 28 '15 at 09:10
  • @GrahamDumpleton I added `logging.info.(time.time())` before and after `get_wsgi_application()`, when I restart the httpd server the log file was created and I found that logged time of start and end was equal. Then I requested the URL from the browser where the time delay happens but there is no log was written to file. – Balakrishnan Jul 28 '15 at 11:53
  • The code at global scope only gets executed once when code file is loaded, not every request. What if you output time at the start of the request? If the difference between start and end of that request is negligible, then issue is outside of mod_wsgi. Possibly issue with Apache configuration and/or DNS name lookup delays, with the latter even perhaps being related to the browser and not Apache. – Graham Dumpleton Jul 28 '15 at 22:02
  • @GrahamDumpleton start time is equal to end time and I'm accessing both the websites via other computers in the same local network. – Balakrishnan Jul 29 '15 at 03:59
  • Personally what I would do next is use a custom Python http client script on the same host to trigger a request and have it also dump out the time the request is made. So work backwards and see if the delay is between the client and WSGI application on that side. Beyond that, the issue may be the lazy loading that Django performs. That is, it will only do some stuff on the first request. One possibility there is that the issue is database connection time as newer Django versions will retain a connection. Thus could be the slow connection on first request for the database. – Graham Dumpleton Jul 29 '15 at 04:29
  • How are you measuring "loading time"? – Burhan Khalid Jul 29 '15 at 06:18
  • @GrahamDumpleton like you said I have written a custom Python script to request `http://one.local.com/` and `http://two.local.com/` by the result of that I found that there is delay only when request from ***browser*** and not from `urllib.request.urlopen(...).read()`. I really don't understand what is happening and also with IE sometimes `http://two.local.com/` is not responding at all. – Balakrishnan Jul 29 '15 at 08:37
  • I haven't seen it in a while, but browsers can get confused localhost isn't set up for both IPv4 and IPv6 addressed. What Windows are you using? Ah, but then you said you were coming from a different host as well, which means that wouldn't apply. – Graham Dumpleton Jul 29 '15 at 10:55

0 Answers0