question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Executed lines reported as missing in threads

See original GitHub issue

Originally reported by k3it (Bitbucket: k3it, GitHub: k3it)


I apologize for a cross post from http://stackoverflow.com/q/43991865/1653558 but it looks like this is a better place for the report

I’m getting some low coverage numbers on script with threads activated with a run() method. It seems that the coverage module is unable to keep track of all hits. Here is the code that demonstrates the problem. Running it should produce 100% coverage, but I’m getting 71.

#!python

from threading import Thread

class MyNestedThread(Thread):
        def run(self):
                print("hello from the nested thread!")

class MyThread(Thread):
        def run(self):
                print("Hello from thread")
                t = MyNestedThread()
                t.start()
                return

if __name__ == '__main__':
        for i in range(3):
                t = MyThread()
                t.start()
#!python


Hello from thread
hello from the nested thread!
Hello from thread
Hello from thread
hello from the nested thread!
hello from the nested thread!

Name     Stmts   Miss  Cover
----------------------------
foo.py      14      4    71%

re-running ‘coverage run foo.py’ sometimes gives a different %, and even 100% on occasion.


Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:41 (22 by maintainers)

github_iconTop GitHub Comments

1reaction
nedbatcommented, Feb 7, 2022

Threads won’t be measured unless you specify them as part of your concurrency setting:

concurrency = multiprocessing,thread
0reactions
JannisNecommented, Feb 7, 2022

Hi, I found this thread when looking for a solution to a problem I wrote up here.

I am also having trouble measuring coverage in threads. I do join() my threads as suggested in this thread so that does not seem to be the problem. You can see the link above for additional info on the environment. I’d be grateful for any help!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Basic python threading is not working. What am I missing in this?
In the line that initializes the thread, you are actually executing the function instead of passing its reference to the thread.
Read more >
Command line usage — Coverage.py 6.4.4 documentation
Here's a sample report. Lines are highlighted green for executed, red for missing, and gray for excluded. The counts at the top of...
Read more >
Manage concurrent threads - Python Module of the Week
Purpose: Builds on the thread module to more easily manage several threads of execution. Available In: 1.5.2 and later ...
Read more >
An Intro to Threading in Python
In this intermediate-level tutorial, you'll learn how to use threading in your Python programs. You'll see how to create threads, how to coordinate...
Read more >
View threads in the Parallel Stacks window - Visual Studio ...
In Threads view, the stack frame and call path of the current thread are highlighted in blue. The current location of the thread...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found