对于应该在Linux和Windows上运行的程序(python2.7),我尝试使用multiprocessing.Process更新给定对象的值(当主程序运行时,我通过一个单独的进程调用update类)。在
有时更新对象需要很长时间,因此我希望能够停止更新过程,并继续执行主程序。”这里并没有严格定义“太长”,而是用户的主观感受。
对于单个队列(如http://pymotw.com/2/multiprocessing/communication.html中的MyFancyClass示例),我可以终止更新进程,主程序可以继续执行。
但是,当我创建第二个队列来检索更新的对象时,结束更新过程不允许我继续主程序。在
到目前为止,我得到的是:import multiprocessing
import time, os
class NewParallelProcess(multiprocessing.Process):
def __init__(self, taskQueue, resultQueue, processName):
multiprocessing.Process.__init__(self)
self.taskQueue = taskQueue
self.resultQueue = resultQueue
self.processName = processName
def run(self):
print "pid %s of process that could be killed" % os.getpid()
while True:
next_task = self.taskQueue.get()
if next_task is None:
# poison pill for terminate
print "%s: exiting" % self.processName
self.taskQueue.task_done()
break
print "%s: %s" % (self.processName, next_task)
answer = next_task()
self.taskQueue.task_done()
self.resultQueue.put(answer)
return
class OldObject(object):
def __init__(self):
self.accurate = "OldValue"
self.otherValue = "SomeOtherValue"
class UpdateObject(dict):
def __init__(self, objectToUpdate):
self.objectToUpdate = objectToUpdate
def __call__(self):
returnDict = {}
returnDict["update"] = self.updateValue("NewValue")
return returnDict
def __str__(self):
return "update starting"
def updateValue(self, updatedValue):
for i in range(5):
time.sleep(1) # updating my object - time consuming with possible pid kill
print "working... (pid=%s)" % os.getpid()
self.objectToUpdate.accurate = updatedValue
return self.objectToUpdate
if __name__ == '__main__':
taskQueue = multiprocessing.JoinableQueue()
resultQueue = multiprocessing.Queue()
newProcess = NewParallelProcess(taskQueue, resultQueue, processName="updateMyObject")
newProcess.start()
myObject = OldObject()
taskQueue.put(UpdateObject(myObject))
# poison pill for NewParallelProcess loop and wait to finish
taskQueue.put(None)
taskQueue.join()
# get back results
results = resultQueue.get()
print "Values have been updated"
print "---> %s became %s" % (myObject.accurate, results["update"].accurate)
关于如何终止newProcess并在主程序中继续运行有什么建议吗?在
嗯,做了一些修改,这就是我想要的。不确定它是否是最有效的,所以任何改进都是受欢迎的:)
^{pr2}$