subprocess.rst 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. .. Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
  2. .. For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt
  3. .. _subprocess:
  4. =======================
  5. Measuring sub-processes
  6. =======================
  7. .. :history: 20100224T201800, new for 3.3.
  8. .. :history: 20100725T211700, updated for 3.4.
  9. Complex test suites may spawn sub-processes to run tests, either to run them in
  10. parallel, or because sub-process behavior is an important part of the system
  11. under test. Measuring coverage in those sub-processes can be tricky because you
  12. have to modify the code spawning the process to invoke coverage.py.
  13. There's an easier way to do it: coverage.py includes a function,
  14. :func:`coverage.process_startup` designed to be invoked when Python starts. It
  15. examines the ``COVERAGE_PROCESS_START`` environment variable, and if it is set,
  16. begins coverage measurement. The environment variable's value will be used as
  17. the name of the :ref:`configuration file <config>` to use.
  18. When using this technique, be sure to set the parallel option to true so that
  19. multiple coverage.py runs will each write their data to a distinct file.
  20. Configuring Python for sub-process coverage
  21. -------------------------------------------
  22. Measuring coverage in sub-processes is a little tricky. When you spawn a
  23. sub-process, you are invoking Python to run your program. Usually, to get
  24. coverage measurement, you have to use coverage.py to run your program. Your
  25. sub-process won't be using coverage.py, so we have to convince Python to use
  26. coverage.py even when not explicitly invoked.
  27. To do that, we'll configure Python to run a little coverage.py code when it
  28. starts. That code will look for an environment variable that tells it to start
  29. coverage measurement at the start of the process.
  30. To arrange all this, you have to do two things: set a value for the
  31. ``COVERAGE_PROCESS_START`` environment variable, and then configure Python to
  32. invoke :func:`coverage.process_startup` when Python processes start.
  33. How you set ``COVERAGE_PROCESS_START`` depends on the details of how you create
  34. sub-processes. As long as the environment variable is visible in your
  35. sub-process, it will work.
  36. You can configure your Python installation to invoke the ``process_startup``
  37. function in two ways:
  38. #. Create or append to sitecustomize.py to add these lines::
  39. import coverage
  40. coverage.process_startup()
  41. #. Create a .pth file in your Python installation containing::
  42. import coverage; coverage.process_startup()
  43. The sitecustomize.py technique is cleaner, but may involve modifying an
  44. existing sitecustomize.py, since there can be only one. If there is no
  45. sitecustomize.py already, you can create it in any directory on the Python
  46. path.
  47. The .pth technique seems like a hack, but works, and is documented behavior.
  48. On the plus side, you can create the file with any name you like so you don't
  49. have to coordinate with other .pth files. On the minus side, you have to
  50. create the file in a system-defined directory, so you may need privileges to
  51. write it.
  52. Note that if you use one of these techniques, you must undo them if you
  53. uninstall coverage.py, since you will be trying to import it during Python
  54. start-up. Be sure to remove the change when you uninstall coverage.py, or use
  55. a more defensive approach to importing it.
  56. Signal handlers and atexit
  57. --------------------------
  58. .. hmm, this isn't specifically about subprocesses, is there a better place
  59. where we could talk about this?
  60. To successfully write a coverage data file, the Python sub-process under
  61. analysis must shut down cleanly and have a chance for coverage.py to run the
  62. ``atexit`` handler it registers.
  63. For example if you send SIGTERM to end the sub-process, but your sub-process
  64. has never registered any SIGTERM handler, then a coverage file won't be
  65. written. See the `atexit`_ docs for details of when the handler isn't run.
  66. .. _atexit: https://docs.python.org/2/library/atexit.html