2014-10-09 63 views
0

檢查這個無文檔threading.Thread子類,需要一點指導。覆蓋線程中的__init__線程

我的班做什麼的理解是:

"""Reload module if it's been updated since last compiled.""" 

代碼:

class Hotswap(threading.Thread): 

    def __init__(self, out, mod, gen='generate', *args, **kwargs): 
     self.out = out # this is an output destination 
     self.mod = mod 
     self.genname = gen # this is a generator from the "mod" 
     self.gen = getattr(mod, self.genname)(*args, **kwargs) 
     self.loaded = self.current_modtime 
     self.args = args 
     self.kwargs = kwargs 

     threading.Thread.__init__(self) 
     self.daemon = True 

    @property 
    def current_modtime(self): 
     return os.path.getmtime(self.mod.__file__.replace("pyc", "py")) 

    def run(self): 
     while True: 
      if self.current_modtime != self.loaded: 
       log.info("Hot-swapping module: %s", self.mod.__name__) 
       self.mod = reload(self.mod) 
       self.loaded = self.current_modtime 
       self.gen = getattr(self.mod, self.genname)(*self.args, **self.kwargs) 
      self.handle(self.gen.next()) 

    def handle(self, elem): 
     self.out(elem) 

這是調用的類的例子:

Hotswap(InfoHandler.add, info, 'generate', info_queue, first_frame).start() 

凡InfoHandler創建一個TornadIO插座,info是一個模塊,generate它的方法和info_queue, first_frame是* args。

我不明白的是out, mod, gen='generate'如何配合threading.Threadgroup=None, target=None, name=None。當threading.Thread.__init__(self)運行時,組,目標和名稱是否初始化爲默認值(無)?

+1

這些都是可選的,所以是的,它們被設置爲它們的默認值是'None'。對'threading.Thread .__ init __(self)'的調用只是調用超類'__init__'與使用'super(Hotswap,self)相同的東西.__ init __()' – Ngenator 2014-10-09 01:49:33

+0

'super'調用將在__init__中,對?而'Hotswap .__ dict__'將返回'Hotswap'和'threading.Thread'的所有屬性。 – MikeiLL 2014-10-09 02:02:20

+0

@Ngenator - 這個問題是否允許即使發佈?想知道我是否應該刪除。它實際上是否會邀請真正的答案? – MikeiLL 2014-10-09 02:26:30

回答

1

如果你看一下threading.Thread的文件,你會看到

threading.Thread(group=None, target=None, name=None, args=(), kwargs={}) 

所以,是的,這些值將設置到None當超__init__被調用。

__dict__問題有點複雜。看看下面的代碼(我用同樣的名字作爲你清晰的問題):

class Thread(object): 
    def __init__(self, a=None): 
     self.a = a 

    def b(self): 
     pass 

class Hotswap(Thread): 
    def __init__(self, x=None): 
     print("Before init: {}".format(self.__dict__)) 

     self.x = x 

     super(Hotswap, self).__init__() 

     print("After init: {}".format(self.__dict__)) 

    def y(self): 
     pass 

print("On Hotswap class: {}".format(Hotswap.__dict__)) 
h = Hotswap() 
print("On Hotswap instance: {}".format(h.__dict__)) 

輸出結果是這樣的:

On Hotswap class: {'y': <function y at 0x24b9bc>, '__module__': '__main__', '__doc__': None, '__init__': <function __init__ at 0x24b97c>} 
Before init: {} 
After init: {'a': None, 'x': None} 
On Hotswap instance: {'a': None, 'x': None} 

正如你所看到的,它沒有考慮到除非你在實例上調用超類(或它自己的)屬性。

+0

所以'class .__ dict__'包含函數和特殊對象,但'instance .__ dict__'只包含屬性? – MikeiLL 2014-10-09 13:15:14

+0

在打印語句中出現錯誤,因此使用'.format()'提交了一個編輯。 – MikeiLL 2014-10-09 13:20:08

+0

在類和類實例部分的底部查看此https://docs.python.org/2/reference/datamodel.html#the-standard-type-hierarchy。 – Ngenator 2014-10-09 13:24:54