Python线程无法启动。

时间:2013-08-17 02:09:52

标签: python multithreading multiprocess

我遇到了halt_listener主题的问题。我可以启动import_1但它不会产生halt_listener个线程。我在已知良好代码之后对此进行模式化,唯一的区别是在最后一次迭代中halt_listener被送入管道而不是队列。

class test_imports:#Test classes remove 
      alive = {'import_1': True, 'import_2': True};

      def halt_listener(self, control_Queue, thread_Name, kill_command):
          while True:
              print ("Checking queue for kill")
              isAlive = control_queue.get()
              print ("isAlive", isAlive)
              if isAlive == kill_command:
                 print ("kill listener triggered")
                 self.alive[thread_Name] = False;
                 return

      def import_1(self, control_Queue, thread_Number):
          print ("Import_1 number %d started") % thread_Number
          t = Thread(target=test_imports.halt_listener, args=(control_Queue, 'import_1', 't1kill'))
          count = 0 
          global alive 
          run = test_imports.alive['import_1'];
          while run:
                print ("Thread type 1 number %d run count %d") % (thread_Number, count)
                count = count + 1
                print ("Test Import_1 ", run)
                run = self.alive['import_1'];
          print ("Killing thread type 1 number %d") % thread_Number 



      def import_2(self, control_queue, thread_number):
          print ("Import_2 number %d started") % thread_number
          count = 1
          while True:
                alive = control_queue.get()                   
                count = count + 1
                if alive == 't2kill':
                   print ("Killing thread type 2 number %d") % thread_number
                   return 
                else:
                     print ("Thread type 2 number %d run count %d") % (thread_number, count)

任何人都有指向我错误的地方。

2 个答案:

答案 0 :(得分:2)

您永远不会致电t.start()来实际启动该主题。

答案 1 :(得分:0)

一切都很好,你只需要添加t.start()来启动你的线程

t = Thread(target=test_imports.halt_listener, args=(control_Queue, 'import_1', 't1kill'))
t.start()