ealeon ealeon - 5 months ago 9
Python Question

Python cant find class attribute

Why is not

Factory.rdyq
visible to inherited classes?

bar.py

import Queue
import threading



class Factory:
name = ""
rdyQ = Queue.Queue()
dispQ = Queue.Queue()

def __init__(self):
self.worker_count = 1
self.worker_thread = None

def worker(self): pass

def initialize(self):
for i in range(self.worker_count):
t = threading.Thread(target=self.worker)
t.daemon = True
t.start()
self.worker_thread = t




@staticmethod
def joinQ():
Factory.rdyQ.join()
Factory.dispQ.join()
return

@staticmethod
def getFactory(factory_name):
if factory_name == "setup":
return SetupFactory()
elif factory_name == "dispatch":
return DispatchFactory()
elif factory_name == "complete":
return CompleteFactory()
else:
return None

class SetupFactory(Factory):
name = "setup"

def worker(self):
while True:
try:
item = Factory.rdyq.get(timeout=1)
print "setup: %s" % item
Factory.rdyq.task_done()

Factory.dispQ.put(item)
except Queue.Empty, msg:
continue

class DispatchFactory(Factory):
name = "dispatch"

def worker(self):
while True:
try:
item = Factory.dispQ.get(timeout=1)
print "dispQ: %s" % item
Factory.dispQ.task_done()

except Queue.Empty, msg:
continue

class CompleteFactory(Factory):
name = "complete"

def worker(self):
for i in range(10):
Factory.rdyq.put(i)


foo.py

import bar as df

setup = df.Factory.getFactory("setup")
dispatch = df.Factory.getFactory("dispatch")
complete = df.Factory.getFactory("complete")

setup.initialize()
dispatch.initialize()
complete.initialize()

df.Factory.joinQ()

setup.worker_thread.join()
dispatch.worker_thread.join()
complete.worker_thread.join()




python foo.py



File "/u/bar.py", line 73, in worker
Factory.rdyq.put(i)
AttributeError: class Factory has no attribute 'rdyq'

Answer

You should use a classmethod instead of a staticmethod for joinQ.

You also had a rdyq (bar.py last line) instead of the expected rdyQ and Python is case-sensitive.

bar.py

import Queue
import threading



class Factory:
    name = ""
    rdyQ = Queue.Queue()
    dispQ = Queue.Queue()

    def __init__(self):
        self.worker_count = 1
        self.worker_thread = None

    def worker(self): pass

    def initialize(self):
        for i in range(self.worker_count):
            t = threading.Thread(target=self.worker)
            t.daemon = True
            t.start()
            self.worker_thread = t




    @classmethod
    def joinQ(cls):
        cls.rdyQ.join()
        cls.dispQ.join()
        return

    @staticmethod
    def getFactory(factory_name):
        if factory_name == "setup":
            return SetupFactory()
        elif factory_name == "dispatch":
            return DispatchFactory()
        elif factory_name == "complete":
            return CompleteFactory()
        else:
            return None

class SetupFactory(Factory):
    name = "setup"

    def worker(self):
        while True:
            try:
                item = Factory.rdyq.get(timeout=1)
                print "setup: %s" % item
                Factory.rdyq.task_done()

                Factory.dispQ.put(item)
            except Queue.Empty, msg:
                continue

class DispatchFactory(Factory):
    name = "dispatch"

    def worker(self):
        while True:
            try:
                item = Factory.dispQ.get(timeout=1)
                print "dispQ: %s" % item
                Factory.dispQ.task_done()

            except Queue.Empty, msg:
                continue

class CompleteFactory(Factory):
    name = "complete"

    def worker(self):
        for i in range(10):
            Factory.rdyQ.put(i)