#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2017
    Posts
    7
    Rep Power
    0

    What does the _MainThread mean?


    Here is the example python code.

    import threading

    class myThread(threading.Thread):
    def __init__(self, threadID, name, counter):
    threading.Thread.__init__(self)
    self.threadID = threadID
    self.name = name
    self.counter = counter

    def run(self):
    currentTreadname = threading.currentThread()
    print "running in", currentTreadname

    thread = myThread(1,"mythrd",1)
    thread.run()
    running in <_MainThread(MainThread, started 139982993762048)>
    thread.start()
    running in <myThread(mythrd, started 139982957491968)>

    What does the _MainThread mean here for thread.run() ?
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,843
    Rep Power
    509
    Let's modify the program slightly and run in python3. We quickly find that the thread number of _MainThread agrees with that of the process in which python3 started.
    Code:
    #! /usr/bin/python3
    
    import threading
    
    class myThread(threading.Thread):
        def __init__(self, threadID, name, counter):
            threading.Thread.__init__(self)
            self.threadID = threadID
            self.name = name
            self.counter = counter
    
        def run(self):
            currentTreadname = threading.currentThread()
            print( "running in", currentTreadname  )
            print( 'threading.main_thread()', threading.main_thread())
    
    
    thread = myThread(1,"mythrd",1)
    thread.run()
    
    thread.start()
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2007
    Location
    US
    Posts
    9
    Rep Power
    0
    It is a class in threading module, right? That's all I know.

    Keep on keepin’ on.
  6. #4
  7. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,843
    Rep Power
    509
    It is a class in the threading module. From /usr/lib/python3.5/threading.py
    Code:
    11 matches for "^class" in buffer: threading.py
         85:class _RLock:
        201:class Condition:
        367:class Semaphore:
        447:class BoundedSemaphore(Semaphore):
        485:class Event:
        564:class Barrier:
        718:class BrokenBarrierError(RuntimeError):
        736:class Thread:
       1156:class Timer(Thread):
       1186:class _MainThread(Thread):
       1205:class _DummyThread(Thread):
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo