setup_nginxuwsgi.py 1.0 KB

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