python running coverage on never ending process
<p>I have a multi processed web server with processes that never end, I would like to check my code coverage on the whole project in a live environment (not only from tests).</p>

<p>The problem is, that since the processes never end, I don't have a good place to set the <code>cov.start() cov.stop() cov.save()</code> hooks.</p>

<p>Therefore, I thought about spawning a thread that in an infinite loop will save and combine the coverage data and then sleep some time, however this approach doesn't work, the coverage report seems to be empty, except from the sleep line.</p>

<p>I would be happy to receive any ideas about how to get the coverage of my code,
or any advice about why my idea doesn't work. Here is a snippet of my code:</p>

<pre><code>import coverage
cov = coverage.Coverage()
import time
import threading
import os

class CoverageThread(threading.Thread):
    _kill_now = False
    _sleep_time = 2

@classmethod
def exit_gracefully(cls):
    cls._kill_now = True

def sleep_some_time(self):
    time.sleep(CoverageThread._sleep_time)

def run(self):
    while True:
        cov.start()
        self.sleep_some_time()
        cov.stop()
        if os.path.exists('.coverage'):
            cov.combine()
        cov.save()
        if self._kill_now:
            break
    cov.stop()
    if os.path.exists('.coverage'):
        cov.combine()
    cov.save()
    cov.html_report(directory="coverage_report_data.html")
    print "End of the program. I was killed gracefully :)"
</code></pre>

<p>Since you are willing to run your code differently for the test, why not add a way to end the process for the test?  That seems like it will be simpler than trying to hack coverage.</p>

python multithreading python-multiprocessing coverage.py