Python无法找到类属性

时间:2016-06-24 13:53:08

标签: python python-multithreading

为什么Factory.rdyq对继承的类不可见?

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'

3 个答案:

答案 0 :(得分:1)

您应该使用classmethod而不是staticmethod来加入。

您还有一个rdyq(bar.py最后一行)而不是预期的rdyQ,而且Python区分大小写。

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)

答案 1 :(得分:1)

Python变量和属性区分大小写,因此“rdyq”与“rdyQ”不同。

答案 2 :(得分:1)

您使用大写q设置名称,所以这可能会解决它吗?

Factory.rdyQ.put(I)

我建议使用using_underscores_for_variables,以避免camelCase可能导致的问题。