Вот код, это фиктивные классы, которые в конечном итоге будут заменены чем-то более полезным. То, что я хочу, чтобы цикл while работал, - это вытащить данные из очереди, чтобы увидеть, была ли удалена таблетка яда. Если нет, я хочу, чтобы все, что было в инструкции else для запуска. Однако по какой-то причине он будет ждать, пока она не получает яд таблетки и только выполнить условие Разрушитель если операторПочему мой оператор python else не запускается?
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()
count = count + 1
if alive == 't1kill':#<==will trigger
print ("Killing thread type 1 number %d") % thread_number
run = False
else:#<== won't trigger
print ("Thread type 1 number %d run count %d") % (thread_number, count)
При необходимости остальная часть кода:
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()
count = count + 1
if alive == 't1kill':
print ("Killing thread type 1 number %d") % thread_number
run = False
else:
print ("Thread type 1 number %d run count %d") % (thread_number, count)
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()
count = count + 1
if alive == 't2kill':
print ("Killing thread type 2 number %d") % thread_number
run = False
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)
Пожалуйста, обратите внимание, import_2
идентичен import_1
кроме печатает что-то другое. Дело в том, чтобы доказать способность обрабатывать разные типы потоков.
Что возвращает control_queue.get()? это строка? объект ? – PepperoniPizza
@PepperoniPizza a string –