Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8454893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:08:37+00:00 2026-06-10T12:08:37+00:00

I’m new to Python so my apologies if this is something obvious. I’m trying

  • 0

I’m new to Python so my apologies if this is something obvious.

I’m trying to build multi threaded application, however when I want to create a thread I get two instead of one.

MyThread.py

from threading import Thread
import time

class MyThreadClass(Thread):

    def __init__(self):
        Thread.__init__(self)

    def run(self):
        print "starting " + self.getName() + "\n"
        from main import var1
        while True:
            print self.getName() + " is running\n"
            print "value: " + var1 + "\n"
            time.sleep(1)

main.py

from MyThread import MyThreadClass
var1 = "Test"
MyThreadClass().start()

The output I get

 Thread-1 is running
 Thread-2 is running
 Thread-1 is running
 Thread-2 is running
 Thread-1 is running
 Thread-2 is running
 .....

Why is it happening? I noticed that if I replace MyThreadClass().start() with MyThreadClass().run() I get 2 threads but only one of them keeps running

 Thread-1 is running
 Thread-2 is running
 Thread-2 is running
 Thread-2 is running
 Thread-2 is running
 .....

Any idea what’s wrong with the code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T12:08:38+00:00Added an answer on June 10, 2026 at 12:08 pm

    When you import main.py in MyThread.py, the line

    MyThreadClass().start()

    gets executed once again (since the module gets loaded), hence a second thread is started.


    You could create a guard clause in main.py by replacing that line with

    if __name__ == "__main__":
        MyThreadClass().run()
    

    or better, just pass var1 to MyThreadClass as a parameter to avoid the circular dependency.

    MyThread.py:

    from threading import Thread
    import time
    
    class MyThreadClass(Thread):
    
        def __init__(self, var1):
            Thread.__init__(self)
            self.var1 = var1
    
        def run(self):
            print "starting " + self.getName() + "\n"
            while True:
                print self.getName() + " is running\n"
                print "value: " + self.var1 + "\n"
                time.sleep(1)
    

    main.py

    from MyThread import MyThreadClass
    
    if __name__ == "__main__":
        MyThreadClass("Test").run()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.