setup_nginxuwsgi.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  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.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. "{0}/uwsgi --ini uwsgi.ini --processes {1} --wsgi app:app".format(
  16. bin_dir, NCPU*3),
  17. shell=True, cwd=CWD, stderr=errfile, stdout=logfile)
  18. return 0
  19. except subprocess.CalledProcessError:
  20. return 1
  21. def stop(logfile, errfile):
  22. subprocess.call(
  23. NGINX_COMMAND + ' -s stop',
  24. shell=True, stdout=logfile, stderr=errfile)
  25. subprocess.call(bin_dir + '/uwsgi --stop /tmp/uwsgi.pid',
  26. shell=True, stderr=errfile, stdout=logfile)
  27. return 0