setup_nginxuwsgi.py 1.1 KB

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