1

Okay, suppose I've got a working class that inherits Thread:

from threading import Thread
import time    

class DoStuffClass(Thread):

    def __init__(self, queue):
        self. queue = queue
        self.isstart = False

    def startthread(self, isstart):
        self.isstart = isstart
        if isstart:
            Thread.__init__(self)
        else:
            print 'Thread not started!'

    def run(self):
        while self.isstart:
            time.sleep(1)
            if self.queue.full():
                y = self.queue.get()    #y goes nowhere, it's just to free up the queue
            self.queue.put('stream data')

I've tried calling it in another file and it's working successfully:

from Queue import Queue
import dostuff

q = Queue(maxsize=1)
letsdostuff= dostuff.DoStuffClass()

letsdostuff.startthread(True)

letsdostuff.start()
val = ''
i=0
while (True):
    val = q.get()
    print "Outputting: %s" % val

Right now, I can get the value of the class output thru the queue.

My question: Suppose I want to create another class (ProcessStuff) that inherits the DoStuffClass so that I can grab the output of DoStuffClass through a queue object (or any other method), process it, and pass it to ProcessStuff's queue so that codes calling the ProcessStuff can get its value through queuing. How do I do that?

Sodrohu
  • 51
  • 3
  • 13
  • Also, please see [this comment](http://stackoverflow.com/questions/25875398/python-threads-always-have-x-active-threads-when-iterating-over-n-tasks#comment40494089_25875398) I made on another question about multithreading. You may be better off using the `multiprocessing` module instead of `threading`, if your threads are doing CPU-bound operations. – dano Sep 18 '14 at 02:53

1 Answers1

3

It sound like you don't really want ProcessStuff to inherit from DoStuffClass, instead you want ProcessStuff to consume from the DoStuffClass queue internally. So rather than use inheritance, just have ProcessStuff keep a reference to a DoStuffClass instance internally, along with an internal Queue object to get the values that DoStuffClass produces:

class ProcessStuff(Thread):
    def __init__(self, queue):
        super(ProcessStuff, self).__init__()
        self.queue = queue
        self._do_queue = Queue()  # internal Queue for DoStuffClass
        self._do_stuff = dostuff.DoStuffClass(self._do_queue)

    def run(self):
        self._do_stuff.startthread(True)
        self._do_stuff.start()
        while True:
            val = self._do_queue.get()  # Grab value from DoStuffClass
            # process it
            processed_val = "processed {}".format(val)
            self.queue.put(processed_val)

q = Queue(maxsize=1)
letsprocessstuff = ProcessStuff(q)

letsprocessstuff.start()
while (True):
    val = q.get()
    print "Outputting: %s" % val

Output:

Outputting: processed stream data
Outputting: processed stream data
Outputting: processed stream data
Outputting: processed stream data
dano
  • 91,354
  • 19
  • 222
  • 219
  • Thanks! This is exactly what I require - couldn't find the exact words when asking for it! I see it uses super() - if I replace it with Thread.__init__(self) it works just as fine. – Sodrohu Sep 18 '14 at 04:42