嗨,我正在创建这个工作生成器类,我在终止时挂起我知道我需要使用 a.join
来关闭它们,但我不知道如何将进程名称传递给terminate
函数。我的想法是以某种方式将其保存到全局变量中,我在想一本字典。然后,当需要终止工作人员时,可以terminate
访问该函数变量并在将毒丸放入消息队列后终止适用的进程
class worker_manager:
i = test_imports()
#someVarForP
#someVarForP2
def generate(control_queue, threadName, runNum):
if threadName == 'one':
print ("Starting import_1 number %d") % runNum
p = multiprocessing.Process(target=i.import_1, args=(control_queue, runNum))
#someVarForP = p
p.start()
if threadName == 'two':
print ("Starting import_2 number %d") % runNum
p = multiprocessing.Process(target=i.import_2, args=(control_queue, runNum))
#someVarForP2 = p2
p.start()
if 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
#someVarForP = p
#someVarForP2 = p2
p.start()
p2.start()
def terminate(threadName):
if threadName == 'one':
#self.someVarForP.join()
if threadName == 'two':
#self.someFarForP2.join()
if threadName == 'three':
#self.someVarForP.join()
#self.someVarForP2.join()
你们的任何想法都将不胜感激我不会坚持任何特定的方式来做到这一点,而且我对 python 有点陌生,所以欢迎任何建议!
编辑:完整代码
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:
alive = control_queue.get()
if alive == 't1kill':
print ("Killing thread type 1 number %d") % thread_number
run = False
break
print ("Thread type 1 number %d run count %d") % (thread_number, count)
count = count + 1
def import_2(self, control_queue, thread_number):
print ("Import_1 number %d started") % thread_number
run = True
count = 1
while run:
alive = control_queue.get()
if alive == 't2kill':
print ("Killing thread type 2 number %d") % thread_number
run = False
break
print ("Thread type 2 number %d run count %d") % (thread_number, count)
count = count + 1
class worker_manager:
# ...
names = {'one': 'import_1', 'two': 'import_2'}
def __init__(self):
self.children = {}
def generate(self, control_queue, threadName, runNum):
name = self.names[threadName]
target = i.getattr(name)
print ("Starting %s number %d") % (name, runNum)
p = multiprocessing.Process(target=target, args=(control_queue, runNum))
self.children[threadName] = p
p.start()
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):
if threadName == 'three':
manager.generate(control, 'one', i)
manager.generate(control, 'two', i)
manager.generate(control, threadName, i)
thread_Count = thread_Count + 1
if threadName == 'three':
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")
if threadName == 'three':
manager.terminate('one')
manager.terminate('two')
else:
manager.terminate(threadName)
仅编辑worker_manager
是实际将要使用的唯一一点。剩下的只是为了开发它只是一个提示。