簡體   English   中英

python NameError:全局名稱未定義

[英]python NameError: global name is not defined

我收到此錯誤NameError: global name 'control_queue' is not defined我已經檢查了control_queue ,我認為我創建正確了。

這是我的代碼:

import multiprocessing 
import time 
from threading import Thread

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
          halt = test_imports()
          t = Thread(target=halt.halt_listener, args=(control_Queue, 'import_1', 't1kill'))
          count = 0 
          t.run()
          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)


class worker_manager:
     def __init__(self):
        self.children = {}

     def generate(self, control_queue, threadName, runNum):
        i = test_imports()
        if threadName == 'one':
            print ("Starting import_1 number %d") % runNum
            p = multiprocessing.Process(target=i.import_1, args=(control_queue, runNum))
            self.children[threadName] = p
            p.start()        
        elif threadName == 'two': 
            print ("Starting import_2 number %d") % runNum
            p = multiprocessing.Process(target=i.import_2, args=(control_queue, runNum))
            self.children[threadName] = p
            p.start()
        elif threadName == 'three':    
            p = multiprocessing.Process(target=i.import_1, args=(control_queue, runNum))
            print ("Starting import_1 number %d") % runNum
            p2 = multiprocessing.Process(target=i.import_2, args=(control_queue, runNum))
            print ("Starting import_2 number %d") % runNum
            self.children[threadName] = p
            self.children[threadName] = p2
            p.start()
            p2.start()

        else:
            print ("Not a valid choice choose one two or three")     

     def terminate(self, threadName):
         self.children[threadName].join


if __name__ == '__main__':
    # Establish communication queues
    control = multiprocessing.Queue()
    manager = worker_manager()

    runNum = int(raw_input("Enter a number: ")) 
    threadNum = int(raw_input("Enter number of threads: "))
    threadName = raw_input("Enter number: ")
    thread_Count = 0

    print ("Starting threads") 

    for i in range(threadNum):
        manager.generate(control, threadName, i)
        thread_Count = thread_Count + 1              

    time.sleep(runNum)#let threads do their thing

    print ("Terminating threads")     

    for i in range(thread_Count):
        control.put("t1kill")
        control.put("t2kill")

    manager.terminate(threadName) 

我認為這是在說我沒有正確創建它,但是我查閱了一些隊列教程,據我所知它是正確的。 有人可以指出我在搞砸嗎? 謝謝伙計們!

在功能halt_listener您的參數是control_Queue但您使用的是變量control_queue (大寫問題)。

control_queue與python中的control_Queue 區分大小寫很重要。 您將參數設置為control_Queue但稍后使用control_queue

這是錯誤的情況。 isAlive = control_queue.get()應該是isAlive = control_Queue.get()

在某些地方發生這種情況

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM