扭曲的逻辑错误

时间:2016-04-17 22:03:28

标签: python logic twisted

我的扭曲程序有效,但现在我的一个反应堆没有优先考虑其他反应堆。我希望controlListener reactor执行一次迭代,然后将优先级传递给printstuffs reactor。

 #Random class as proof of concept
 class printStuffs(object):  
         print "counting "
         printerCount = 0
         def count(self):
             self.printerCount = self.printerCount + 1
             print ("the counter is at " + str(self.printerCount))

  ##########################################################################
  ## The control listneer class is designed to kill given reactor threads ##
  ## on demand from something once it recieves a signal it is supposed    ##
  ## to do one ieteration then release                                    ##
  ##########################################################################

  class controlListener(object):
          counter = 20
          def count(self):
               if self.counter == 0:
                  print "Killing Process"
                  reactor.stop()
              else:
                  print self.counter, '...'
                  self.counter -= 1
                  reactor.callLater(1, self.count)

 from twisted.internet import reactor

 print "Printing random stuff"
 reactor.callWhenRunning(printStuffs().count)

 print "Intializing kill listner"
 reactor.callWhenRunning(controlListener().count)
 reactor.run()

 print "Process killed"

这是输出

  Printing random stuff
  Intializing kill listner
  the counter is at 1
  20 ...
  19 ...
  18 ...
  17 ...
  16 ...
  15 ...
  14 ...
  13 ...
  12 ...
  11 ...
  10 ...
  9 ...
  8 ...
  7 ...
  6 ...
  5 ...
  4 ...
  3 ...
  2 ...
  1 ...
  Killing Process
  Process killed

我希望它能做类似

的事情
 the counter is at 1 
 20 ...
 the counter is at 2 
 the counter is at 3
 19 ...

等。

有什么想法吗?

1 个答案:

答案 0 :(得分:6)

您只是忘了printStuffs.count()使用reactor.callLater()重新安排自己,就像controlListener.count()一样。

class printStuffs(object):  
    printerCount = 0
    def count(self):
        self.printerCount = self.printerCount + 1
        print ("the counter is at " + str(self.printerCount))
        reactor.callLater(1, self.count)

此外,将print语句(print "counting")直接放在类定义中而不是函数中会导致它在python解释器读取类定义时正确运行。这是误导性的,因为消息说“计数”但当时没有发生任何事情(还有)。

这可能是那些如果一个人看不到它,就看不到它的人之一 这就是为什么对于一些重要的函数或线程,我将跟踪日志记录语句添加到我的代码中,告诉我何时调用函数,或者线程在结束时启动。这对于可能由于错误而中止的函数以及您希望在大多数时间运行的线程特别有用。

这是你可以如何调整这个模式的例子:

class printStuffs(object):
    printerCount = 0
    def count(self):
        try:
            ##print "Entering printStuffs.count()."
            self.printerCount = self.printerCount + 1
            print ("The counter is at " + str(self.printerCount))
            # Run again later.
            reactor.callLater(1, self.count)
        except:
            # We won't run again later.
            print "Error in printStuffs.count(), won't run again:", sys.exc_info()[0]
            # Don't swallow the exception.
            raise
        finally:
            ##print "Leaving printStuffs.count()."

当然,这对你的例子来说太过分了,但你真正的代码可能更复杂。

当您的程序变得更大,更复杂时,以这种方式使用日志记录可以帮助您验证程序中的基本过程是否按预期工作。