2016-02-12 46 views
1

我想在seperte进程中启动ActorCore方法,然后处理发送到该ActorCore的消息。出于某种原因,此代码无法正常工作。Python:如何在单独的进程中调用方法

import queue 
from multiprocessing import Process 


class NotMessage(Exception): 
    def __str__(self): 
     return 'NotMessage exception' 


class Message(object): 

    def Do(self, Actor): 
     # Do some stuff to the actor 
     pass 

    def __str__(self): 
     return 'Generic message' 


class StopMessage(Message): 

    def Do(self, Actor): 
     Actor.__stopped = True 

    def __str__(self): 
     return 'Stop message' 


class Actor(object): 
    __DebugName = '' 
    __MsgQ = None 
    __stopped = False 

    def __init__(self, Name): 
     self.__DebugName = Name 
     self.__MsgQ = queue.Queue() 

    def LaunchActor(self): 
     p = Process(target=self.ActorCore) 
     p.start() 
     return self.__MsgQ 

    def ActorCore(self): 
     while not self.__stopped: 
      Msg = self.__MsgQ.get(block=True) 
      try: 
       Msg.Do(self) 
       print(Msg) 
      except NotMessage as e: 
       print(str(e), ' occurred in ', self.__DebugName) 


def main(): 
    joe = Actor('Joe') 
    msg = Message() 
    stop = StopMessage() 
    qToJoe = joe.LaunchActor() 
    qToJoe.put(msg) 
    qToJoe.put(msg) 
    qToJoe.put(stop) 

if __name__ == '__main__': 
    main() 

运行时,我得到奇怪的错误:

Traceback (most recent call last): 
    File "C:/Users/plkruczp/PycharmProjects/ActorFramework/Actor/Actor.py", line 64, in <module> 
    main() 
    File "C:/Users/plkruczp/PycharmProjects/ActorFramework/Actor/Actor.py", line 58, in main 
    qToJoe = joe.LaunchActor() 
    File "C:/Users/plkruczp/PycharmProjects/ActorFramework/Actor/Actor.py", line 40, in LaunchActor 
    p.start() 
    File "C:\Program Files\Python35\lib\multiprocessing\process.py", line 105, in start 
    self._popen = self._Popen(self) 
    File "C:\Program Files\Python35\lib\multiprocessing\context.py", line 212, in _Popen 
    return _default_context.get_context().Process._Popen(process_obj) 
    File "C:\Program Files\Python35\lib\multiprocessing\context.py", line 313, in _Popen 
    return Popen(process_obj) 
    File "C:\Program Files\Python35\lib\multiprocessing\popen_spawn_win32.py", line 66, in __init__ 
    reduction.dump(process_obj, to_child) 
    File "C:\Program Files\Python35\lib\multiprocessing\reduction.py", line 59, in dump 
    ForkingPickler(file, protocol).dump(obj) 
TypeError: can't pickle _thread.lock objects 

请帮助!我什么都试过:(

回答

1

只需使用队列,而不是队列:

删除import queue并添加Queuefrom multiprocessing像:

from multiprocessing import Process,Queue

然后更改self.__MsgQ = queue.Queue()self.__MsgQ = Queue()

这就是你需要做才能让它起作用,其余的对于你的情况是一样的。

编辑,解释:

queue.Queue仅仅是线程安全的,并且multiprocessing不实际产生另一个进程。因此,额外的multiprocessing.Queue被实现为也是过程安全的。作为另一种选择,如果多线程被通缉,该threading库可以一起queue.Queue使用:https://docs.python.org/dev/library/threading.html#module-threading

其他信息:

另一个并行选项,根据您的特殊要求是joblib,在那里产卵可定义为一个进程或线程:https://pythonhosted.org/joblib/

+0

谢谢,它的工作原理。这里有什么问题?是否队列不是线程安全的,但队列是? – Kruczkowski

+0

queue.Queue只有线程安全,而multiprocessing.Queue既是线程安全的,也是过程安全的。不要混淆线程和进程。对于线程,你需要“线程化”而不是“multiprocessing”(抱歉多次编辑,我在第二次之前出错了) – bluesceada

相关问题