pythonnameerror所有情况_python NameError:未定义全局名称

我得到了这个错误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)

我想这是在说我没有正确地创建它,但是我看了一些队列教程,就我所知它是正确的。有人能指出我在哪里搞砸了吗?谢谢你们!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值