Exemple #1
0
    def __init__(self, constructor, strategy='thread'):
        """Step environment in a separate process for lock free parallelism.

    The environment will be created in the external process by calling the
    specified callable. This can be an environment class, or a function
    creating the environment and potentially wrapping it. The returned
    environment should not access global variables.

    Args:
      constructor: Callable that creates and returns an OpenAI gym environment.

    Attributes:
      observation_space: The cached observation space of the environment.
      action_space: The cached action space of the environment.
    """
        if strategy == 'thread':
            import multiprocessing.dummy as mp
        elif strategy == 'process':
            import multiprocessing as mp
        else:
            raise NotImplementedError(strategy)
        self._conn, conn = mp.Pipe()
        self._process = mp.Process(target=self._worker,
                                   args=(constructor, conn))
        atexit.register(self.close)
        self._process.start()
        self._observ_space = None
        self._action_space = None
def checkLiveness(pID, pStub, obj_type, timeout=5):
    """ Used to detect if a peer is still alive. """
    logging.debug("Confirming connection to peer {}.".format(pID))
    try:
        parent_conn, child_conn = multiprocessing.Pipe(duplex=False)
        p = multiprocessing.Process(target=_get_line,
                                    args=(child_conn, pID, pStub, obj_type))
        p.daemon = True
        p.start()
        it_did_timeout = (not parent_conn.poll(timeout))
        if it_did_timeout:
            logging.info("Connection to peer {} timed out.".format(pID))
            return False
        else:
            parent_said_yes = parent_conn.recv()
            if not parent_said_yes:
                logging.info(
                    "No connection to peer {} established.".format(pID))
            return parent_said_yes
    except:
        err = sys.exc_info()
        logging.debug(
            "Encountered an error while spawning a process to check if peer {} is still alive:\n{}: {}"
            .format(pID, err[0], err[1]))
        return False
 def start(self):
     for _ in range(self.size):
         pipe = mp.Pipe()
         p = mp.Process(target=Actor,
                        args=(pipe[1], self.actor_device, (global_config,
                                                           actor_config),
                              self.ground_truth))
         p.start()
         self.actors.append((p, pipe[0]))
Exemple #4
0
 def start_listen_threaded(self):
     inp, out = thr.Pipe()
     process = thr.Process(target=self.run_function,
                           args=(inp, ),
                           name=self.name)
     process.start()
     while True:
         time.sleep(0.01)
         text = input("Console input>>>")
         self.send(text)
Exemple #5
0
 def __init__(self, constructor, strategy='thread'):
   if strategy == 'thread':
     import multiprocessing.dummy as mp
   elif strategy == 'process':
     import multiprocessing as mp
   else:
     raise NotImplementedError(strategy)
   self._strategy = strategy
   self._conn, conn = mp.Pipe()
   self._process = mp.Process(target=self._worker, args=(constructor, conn))
   atexit.register(self.close)
   self._process.start()
   self._observ_space = None
   self._action_space = None
Exemple #6
0
 def __init__(self, ctor, strategy="process"):
     self._strategy = strategy
     if strategy == "none":
         self._env = ctor()
     elif strategy == "thread":
         import multiprocessing.dummy as mp
     elif strategy == "process":
         import multiprocessing as mp
     else:
         raise NotImplementedError(strategy)
     if strategy != "none":
         self._conn, conn = mp.Pipe()
         self._process = mp.Process(target=self._worker, args=(ctor, conn))
         atexit.register(self.close)
         self._process.start()
     self._obs_space = None
     self._action_space = None
Exemple #7
0
 def __init__(self, constructor, strategy='thread'):
   self._pickled_ctor = cloudpickle.dumps(constructor)
   if strategy == 'process':
     import multiprocessing as mp
     context = mp.get_context('spawn')
   elif strategy == 'thread':
     import multiprocessing.dummy as context
   else:
     raise NotImplementedError(strategy)
   self._strategy = strategy
   self._conn, conn = context.Pipe()
   self._process = context.Process(target=self._worker, args=(conn,))
   atexit.register(self.close)
   self._process.start()
   self._receive()  # Ready.
   self._obs_space = None
   self._act_space = None
 def _is_alive(self, obj_type, peer, timeout=5):
     try:
         parent_conn, child_conn = multiprocessing.Pipe(duplex=False)
         p = multiprocessing.Process(target=self._get_line,
                                     args=(child_conn, peer, obj_type))
         p.daemon = True
         p.start()
         it_did_timeout = (not parent_conn.poll(timeout))
         if it_did_timeout:
             logging.info("Connection to peer {} timed out.".format(peer))
             return False
         else:
             parent_said_yes = parent_conn.recv()
             if not parent_said_yes:
                 logging.info(
                     "No connection to peer {} established.".format(peer))
             return parent_said_yes
     except:
         err = sys.exc_info()
         logging.debug(
             "NameServer encountered an error while spawning a process to check if peer {} is still alive:\n{}: {}"
             .format(peer, err[0], err[1]))
         return False