Python函数赢得';不能完全执行
这是一个虚拟类,用于测试多进程生成器。传递消息需要一个队列和一个数字,该数字指示线程被调用的次数。 由于某些原因,我无法在while循环中获得要执行的第一次打印Python函数赢得';不能完全执行,python,logic,Python,Logic,这是一个虚拟类,用于测试多进程生成器。传递消息需要一个队列和一个数字,该数字指示线程被调用的次数。 由于某些原因,我无法在while循环中获得要执行的第一次打印print(“线程类型1编号%d运行计数%d”)%(线程编号,计数)不会运行,但print(“终止线程类型1编号%d”)%Thread\u编号会运行 class test_imports:#Test classes remove def import_1(self, control_queue, thread_number)
print(“线程类型1编号%d运行计数%d”)%(线程编号,计数)
不会运行,但print(“终止线程类型1编号%d”)%Thread\u编号
会运行
class test_imports:#Test classes remove
def import_1(self, control_queue, thread_number):
print ("Import_1 number %d started") % thread_number# Does run
run = True
count = 1
while run == True:
alive = control_queue.get()
print ("Thread type 1 number %d run count %d") % (thread_number, count) #wont run
count = count + 1
if alive == 't1kill':
print ("Killing thread type 1 number %d") % thread_number # does run
run = False
break
这是完整的代码
import multiprocessing
import time
class test_imports:#Test classes remove
def import_1(self, control_queue, thread_number):
print ("Import_1 number %d started") % thread_number
run = True
count = 1
while run == True:
alive = control_queue.get()
print ("Thread type 1 number %d run count %d") % (thread_number, count)
count = count + 1
if alive == 't1kill':
print ("Killing thread type 1 number %d") % thread_number
run = False
break
def import_2(self, control_queue, thread_number):
print ("Import_2 number %d started") % thread_number
run = True
count = 1
while run == True:
alive = control_queue.get()
print ("Thread type 2 number %d run count %d") % (thread_number, count)
count = count + 1
if alive == 't2kill':
print ("Killing thread type 2 number %d") % thread_number
run = False
break
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)
发布其余的代码。一定是control_queue.get()从未返回,是吗?您是否使用了调试器?我猜control_队列是空的,因此它会阻止等待生产者向其添加内容。而且,
run=True;而run==True
看起来有点伤脑筋。你甚至没有使用你正在玩弄的旗帜。只要使用而True
和break
@CodieCodeMonkey,但是如果control\u queue.get()没有返回,第三个打印就不会出现了吗?@CharlesMarsh,对,没有抓住这一点。嗯,看起来不可能!不过,您是否使用了调试器?