process.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. # Copyright (C) 2010 CZ NIC
  2. #
  3. # Permission to use, copy, modify, and distribute this software for any
  4. # purpose with or without fee is hereby granted, provided that the above
  5. # copyright notice and this permission notice appear in all copies.
  6. #
  7. # THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
  8. # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  9. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  10. # INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  12. # FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  13. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  14. # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. """
  16. Module to manipulate the python processes.
  17. It contains only function to rename the process, which is currently
  18. wrapper around setproctitle library. Does not fail if the setproctitle
  19. module is missing, but does nothing in that case.
  20. """
  21. try:
  22. from setproctitle import setproctitle
  23. except ImportError:
  24. def setproctitle(_): pass
  25. import sys
  26. import os.path
  27. """
  28. Rename the current process to given name (so it can be found in ps).
  29. If name is None, use zero'th command line argument.
  30. """
  31. def rename(name=None):
  32. if name is None:
  33. name = os.path.basename(sys.argv[0])
  34. setproctitle(name)