setup_nginx.py 1.2 KB

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