简体   繁体   中英

Python threading in multi subclasses

I have a series of classes that inherit from in a series and parallel manner and I need to use Python threading for all classes when possible. An example is below. The problem is that the Build class does not get its run function executed which is a method in the Thread class. Threading works fine in MyThread class though. Any idea how to make the Build class starts as a thread?

from threading import Thread
from random import randint
import time

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

    def run(self):    
        # This run function currently not being executed 
        for i in range(20):
            print('Second series %i in thread' % (i))
            time.sleep(1)

class MyThread(Build, Thread):

    def __init__(self, val):
        ''' Constructor. '''
        Thread.__init__(self)
        Build.__init__(self)

        self.val = val


    def run(self):
        for i in range(1, self.val):
            print('Value %d in thread %s' % (i, self.getName()))

            # Sleep for random time between 1 ~ 3 second
            secondsToSleep = randint(1, 5)
            print('%s sleeping fo %d seconds...' % (self.getName(), secondsToSleep))
            time.sleep(secondsToSleep)


# Run following code when the program starts
if __name__ == '__main__':
    # Declare objects of MyThread class
    myThreadOb1 = MyThread(4)
    myThreadOb1.setName('Thread 1')

    myThreadOb2 = MyThread(4)
    myThreadOb2.setName('Thread 2')

    # Start running the threads!
    myThreadOb1.start()
    myThreadOb2.start()

    # Wait for the threads to finish...
    myThreadOb1.join()
    myThreadOb2.join()

    print('Main Terminating...')`

FYI: Instead of subclassing threading.Thread the better way to achieve what you want is to make your class instances Callable and just pass them to the target keyword arg of the Thread class' constructor. The advantage of doing this is you can pass in additional arguments to each Thread instance.

going with your sample code.

class MyThread(Build):

    def __init__(self):
        ''' Constructor. '''
        Build.__init__(self)

        self.val = val

    # this allows your class to be a callable.
    def __call__(self, val):
        for i in range(1, val):
            print('Value %d in thread %s' % (i, self.getName()))

            # Sleep for random time between 1 ~ 3 second
            secondsToSleep = randint(1, 5)
            print('%s sleeping fo %d seconds...' % (self.getName(), secondsToSleep))
            time.sleep(secondsToSleep)

# Run following code when the program starts
if __name__ == '__main__':
    # Declare objects of MyThread class
    myThreadObj1 = MyThread()
    myThread1 = Thread(target=myThreadOb1, args=(4))
    myThread1.start()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM