setup_nginxuwsgi.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import subprocess
  2. import multiprocessing
  3. import os
  4. CWD = os.path.abspath(os.path.dirname(__file__))
  5. bin_dir = os.path.expandvars('$PY2_ROOT/bin')
  6. NCPU = multiprocessing.cpu_count()
  7. NGINX_COMMAND = 'sudo /usr/local/nginx/sbin/nginx -c ' + CWD + '/nginx.conf'
  8. def start(args, logfile, errfile):
  9. try:
  10. subprocess.check_call(
  11. NGINX_COMMAND,
  12. shell=True, stdout=logfile, stderr=errfile)
  13. # Run in the background, but keep stdout/stderr for easy debugging
  14. subprocess.Popen(
  15. bin_dir + '/uwsgi --ini uwsgi.ini' +
  16. ' --processes ' + str(NCPU) +
  17. ' --wsgi hello:app',
  18. shell=True, cwd=CWD, stdout=logfile, stderr=errfile)
  19. return 0
  20. except subprocess.CalledProcessError:
  21. return 1
  22. def stop(logfile, errfile):
  23. subprocess.check_call(
  24. NGINX_COMMAND + ' -s stop',
  25. shell=True, stdout=logfile, stderr=errfile)
  26. subprocess.call(
  27. bin_dir + '/uwsgi --stop /tmp/uwsgi.pid',
  28. shell=True, cwd=CWD, stderr=errfile, stdout=logfile)
  29. return 0