# Path: pyactor/green_thread/channel.py
# class Channel(Queue):
#     """
#     Channel is the main communication mechanism between actors. It is
#     actually a simple facade to the gevent.Queue class.
#     """
# 
#     def __init__(self):
#         Queue.__init__(self)
# 
#     def send(self, msg):
#         """
#         It sends a message to the current channel.
# 
#         :param msg: The message sent to an actor. It is a dictionary
#             using the constants in util.py (:mod:`pyactor.util`).
#         """
#         self.put(msg)
# 
#     def receive(self, timeout=None):
#         """
#         It receives a message from the channel, blocking the calling
#         thread until the response is received, or the timeout is
#         triggered.
# 
#         :param int timeout: timeout to wait for messages. If none
#             provided it will block until a message arrives.
#         :return: returns a message sent to the channel. It is a
#             dictionary the constants in util.py (:mod:`pyactor.util`).
#         """
#         return self.get(timeout=timeout)
# 
# Path: pyactor/green_thread/future.py
# class FutureManager(object):
#     """
#     A manager that controls the creation and execution of the futures in a host.
#     """
# 
#     def __init__(self):
#         self.running = False
#         self.channel = Channel()
#         self.futures = {}
#         self.t = None
# 
#     def __queue_management(self):
#         self.running = True
#         while self.running:
#             response = self.channel.receive()
#             if response == 'stop':
#                 self.running = False
#             else:
#                 result = response[RESULT]
#                 future = self.futures[response[RPC_ID]]
#                 if isinstance(result, Exception):
#                     future.set_exception(result)
#                 else:
#                     future.set_result(result)
# 
#     def new_future(self, future_ref, ref=False):
#         future_id = str(uuid.uuid4())
#         if not ref:
#             future = Future(future_id, future_ref, self.channel)
#         else:
#             future = FutureRef(future_id, future_ref, self.channel)
#         future.send_work()
#         self.futures[future_id] = future
# 
#         if not self.running:
#             self.t = spawn(self.__queue_management)
#         return future
# 
#     def stop(self):
#         self.channel.send('stop')
#         if self.t is not None:
#             self.t.join()
#             self.t = None
#         self.futures = {}
# 
# Path: pyactor/util.py
# ASK = 'ASK'
# 
# TELL = 'TELL'
# 
# FUTURE = 'FUTURE'
# 
# TYPE = 'TYPE'
# 
# ASK_RESPONSE = 'ASK_RESPONSE'
# 
# FUTURE_RESPONSE = 'FUTURE_RESPONSE'
# 
# Path: pyactor/util.py
# METHOD = 'METHOD'
# 
# PARAMS = 'PARAMS'
# 
# RESULT = 'RESULT'
# 
# CHANNEL = 'CHANNEL'
# 
# RPC_ID = 'RPC_ID'
# 
# Path: pyactor/util.py
# def ref_l(self, f):
#     def wrap_ref_l(*args):
#         new_args = list(args)
#         try:
#             if new_args[0][METHOD] in self._ref:
#                 new_args[0][PARAMS] = get_host().loads(list(args[0][PARAMS]))
#             return f(*new_args)
#         except HostError:
#             pass
#             # If there is a problem deserializing the params, the method
#             # is not executed.
#     return wrap_ref_l
# 
# def ref_d(self, f):
#     def wrap_ref_d(*args):
#         new_args = list(args)
#         if new_args[1][METHOD] in self._ref:
#             new_args[0] = get_host().dumps(args[0])
#         return f(*new_args)
#     return wrap_ref_d


from copy import copy
from gevent import spawn
from .channel import Channel
from .future import FutureManager
from ..util import ASK, TELL, FUTURE, TYPE, ASK_RESPONSE, FUTURE_RESPONSE
from ..util import METHOD, PARAMS, RESULT, CHANNEL, RPC_ID
from ..util import ref_l, ref_d
    def __process_queue(self):
        while self.running:
            message = self.channel.receive()
            self.receive(message)

    def is_alive(self):
        """
        :return: (*bool.*) identifies the current state of the actor.
            **True** if it is running.
        """
        return self.running

    def receive(self, msg):
        """
        The message received from the queue specifies a method of the
        class the actor represents. This invokes it. If the
        communication is an ASK, sends the result back
        to the channel included in the message as an ASK_RESPONSE.

        If it is a FUTURE, generates a FUTURE_RESPONSE
        to send the result to the manager.

        :param msg: The message is a dictionary using the constants
            defined in util.py (:mod:`pyactor.util`).
        """
        if msg[TYPE] == TELL and msg[METHOD] == 'stop':
            self.running = False
            self.future_manager.stop()
        else:
            try:

# Path: pyactor/green_thread/channel.py
# class Channel(Queue):
#     """
#     Channel is the main communication mechanism between actors. It is
#     actually a simple facade to the gevent.Queue class.
#     """
# 
#     def __init__(self):
#         Queue.__init__(self)
# 
#     def send(self, msg):
#         """
#         It sends a message to the current channel.
# 
#         :param msg: The message sent to an actor. It is a dictionary
#             using the constants in util.py (:mod:`pyactor.util`).
#         """
#         self.put(msg)
# 
#     def receive(self, timeout=None):
#         """
#         It receives a message from the channel, blocking the calling
#         thread until the response is received, or the timeout is
#         triggered.
# 
#         :param int timeout: timeout to wait for messages. If none
#             provided it will block until a message arrives.
#         :return: returns a message sent to the channel. It is a
#             dictionary the constants in util.py (:mod:`pyactor.util`).
#         """
#         return self.get(timeout=timeout)
# 
# Path: pyactor/green_thread/future.py
# class FutureManager(object):
#     """
#     A manager that controls the creation and execution of the futures in a host.
#     """
# 
#     def __init__(self):
#         self.running = False
#         self.channel = Channel()
#         self.futures = {}
#         self.t = None
# 
#     def __queue_management(self):
#         self.running = True
#         while self.running:
#             response = self.channel.receive()
#             if response == 'stop':
#                 self.running = False
#             else:
#                 result = response[RESULT]
#                 future = self.futures[response[RPC_ID]]
#                 if isinstance(result, Exception):
#                     future.set_exception(result)
#                 else:
#                     future.set_result(result)
# 
#     def new_future(self, future_ref, ref=False):
#         future_id = str(uuid.uuid4())
#         if not ref:
#             future = Future(future_id, future_ref, self.channel)
#         else:
#             future = FutureRef(future_id, future_ref, self.channel)
#         future.send_work()
#         self.futures[future_id] = future
# 
#         if not self.running:
#             self.t = spawn(self.__queue_management)
#         return future
# 
#     def stop(self):
#         self.channel.send('stop')
#         if self.t is not None:
#             self.t.join()
#             self.t = None
#         self.futures = {}
# 
# Path: pyactor/util.py
# ASK = 'ASK'
# 
# TELL = 'TELL'
# 
# FUTURE = 'FUTURE'
# 
# TYPE = 'TYPE'
# 
# ASK_RESPONSE = 'ASK_RESPONSE'
# 
# FUTURE_RESPONSE = 'FUTURE_RESPONSE'
# 
# Path: pyactor/util.py
# METHOD = 'METHOD'
# 
# PARAMS = 'PARAMS'
# 
# RESULT = 'RESULT'
# 
# CHANNEL = 'CHANNEL'
# 
# RPC_ID = 'RPC_ID'
# 
# Path: pyactor/util.py
# def ref_l(self, f):
#     def wrap_ref_l(*args):
#         new_args = list(args)
#         try:
#             if new_args[0][METHOD] in self._ref:
#                 new_args[0][PARAMS] = get_host().loads(list(args[0][PARAMS]))
#             return f(*new_args)
#         except HostError:
#             pass
#             # If there is a problem deserializing the params, the method
#             # is not executed.
#     return wrap_ref_l
# 
# def ref_d(self, f):
#     def wrap_ref_d(*args):
#         new_args = list(args)
#         if new_args[1][METHOD] in self._ref:
#             new_args[0] = get_host().dumps(args[0])
#         return f(*new_args)
#     return wrap_ref_d


# Path: pyactor/green_thread/actor.py
from copy import copy
from gevent import spawn
from .channel import Channel
from .future import FutureManager
from ..util import ASK, TELL, FUTURE, TYPE, ASK_RESPONSE, FUTURE_RESPONSE
from ..util import METHOD, PARAMS, RESULT, CHANNEL, RPC_ID
from ..util import ref_l, ref_d

    def __process_queue(self):
        while self.running:
            message = self.channel.receive()
            self.receive(message)

    def is_alive(self):
        """
        :return: (*bool.*) identifies the current state of the actor.
            **True** if it is running.
        """
        return self.running

    def receive(self, msg):
        """
        The message received from the queue specifies a method of the
        class the actor represents. This invokes it. If the
        communication is an ASK, sends the result back
        to the channel included in the message as an ASK_RESPONSE.

        If it is a FUTURE, generates a FUTURE_RESPONSE
        to send the result to the manager.

        :param msg: The message is a dictionary using the constants
            defined in util.py (:mod:`pyactor.util`).
        """
        if msg[TYPE] == TELL and msg[METHOD] == 'stop':
            self.running = False
            self.future_manager.stop()
        else:
            try:

                invoke = getattr(self._obj, msg[METHOD])