Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

workertmp.py 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # -*- coding: utf-8 -
  2. #
  3. # This file is part of gunicorn released under the MIT license.
  4. # See the NOTICE for more information.
  5. import os
  6. import platform
  7. import tempfile
  8. from gunicorn import util
  9. PLATFORM = platform.system()
  10. IS_CYGWIN = PLATFORM.startswith('CYGWIN')
  11. class WorkerTmp(object):
  12. def __init__(self, cfg):
  13. old_umask = os.umask(cfg.umask)
  14. fdir = cfg.worker_tmp_dir
  15. if fdir and not os.path.isdir(fdir):
  16. raise RuntimeError("%s doesn't exist. Can't create workertmp." % fdir)
  17. fd, name = tempfile.mkstemp(prefix="wgunicorn-", dir=fdir)
  18. # allows the process to write to the file
  19. util.chown(name, cfg.uid, cfg.gid)
  20. os.umask(old_umask)
  21. # unlink the file so we don't leak tempory files
  22. try:
  23. if not IS_CYGWIN:
  24. util.unlink(name)
  25. self._tmp = os.fdopen(fd, 'w+b', 1)
  26. except:
  27. os.close(fd)
  28. raise
  29. self.spinner = 0
  30. def notify(self):
  31. try:
  32. self.spinner = (self.spinner + 1) % 2
  33. os.fchmod(self._tmp.fileno(), self.spinner)
  34. except AttributeError:
  35. # python < 2.6
  36. self._tmp.truncate(0)
  37. os.write(self._tmp.fileno(), b"X")
  38. def last_update(self):
  39. return os.fstat(self._tmp.fileno()).st_ctime
  40. def fileno(self):
  41. return self._tmp.fileno()
  42. def close(self):
  43. return self._tmp.close()