Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is my main thread not completing its task?

In the below code the daemon thread is only giving the output whereas the main thread is not. If the main thread works independently of the "x"(daemon) thread then why isn’t it asking for input(it is just giving the output of the daemon thread).

import threading
import time
def daemon_thread():
    print()
    timer = 0
    while True:
        time.sleep(1)
        timer += 1
        print(timer, "s")

x = threading.Thread(target=daemon_thread(), args=())
x.start()
answer = input("Do you wish to exit?")
print(threading.active_count())

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

For the Thread constructor you should pass a reference to the appropriate function. You do not call the function. Therefore:

x = threading.Thread(target=daemon_thread)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading