setup_nginx.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import subprocess
  2. import multiprocessing
  3. import os
  4. CWD = os.path.abspath(os.path.dirname(__file__))
  5. uwsgi = os.path.expandvars('$PY2_ROOT/bin/uwsgi')
  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. # Note that this uses --gevent 1000 just like setup.py.
  15. subprocess.Popen(
  16. uwsgi + ' --ini uwsgi.ini' +
  17. ' --processes ' + str(NCPU) +
  18. ' --gevent 1000 --wsgi hello',
  19. shell=True, cwd='uwsgi',
  20. stdout=logfile, stderr=errfile)
  21. return 0
  22. except subprocess.CalledProcessError:
  23. return 1
  24. def stop(logfile, errfile):
  25. subprocess.call(
  26. NGINX_COMMAND + ' -s stop',
  27. shell=True, stdout=logfile, stderr=errfile)
  28. subprocess.call(
  29. uwsgi + ' --stop /tmp/uwsgi.pid',
  30. shell=True, cwd="uwsgi", stderr=errfile, stdout=logfile)
  31. os.system('killall nginx')
  32. os.system('killall uwsgi')
  33. return 0