diff --git a/python/unityagents/environment.py b/python/unityagents/environment.py index 22349ce021..2f39afa71c 100755 --- a/python/unityagents/environment.py +++ b/python/unityagents/environment.py @@ -7,7 +7,6 @@ import os import socket import subprocess -import signal from .brain import BrainInfo, BrainParameters from .exception import UnityEnvironmentException, UnityActionException @@ -76,28 +75,27 @@ def __init__(self, file_name, worker_id=0, except os.error: self.close() raise UnityEnvironmentException("Couldn't launch new environment. " - "Provided filename does not match any \environments in {}." - .format(cwd)) + "Provided filename does not match any \environments in {}." + .format(cwd)) - def timeout_handler(): - raise UnityEnvironmentException( + self._socket.settimeout(30) + try: + try: + self._socket.listen(1) + self._conn, _ = self._socket.accept() + self._conn.setblocking(1) + p = self._conn.recv(self._buffer_size).decode('utf-8') + p = json.loads(p) + except socket.timeout as e: + raise UnityEnvironmentException( "The Unity environment took too long to respond. Make sure {} does not need user interaction to launch " - "and that the Academy and the external Brain(s) scripts are attached to objects in the Scene.".format( + "and that the Academy and the external Brain(s) are attached to objects in the Scene.".format( str(file_name))) - - old_handler = signal.signal(signal.SIGALRM, timeout_handler) - signal.alarm(30) # trigger alarm in x seconds - try: - self._socket.listen(1) - self._conn, _ = self._socket.accept() - p = self._conn.recv(self._buffer_size).decode('utf-8') - p = json.loads(p) except UnityEnvironmentException: proc1.kill() self.close() raise - signal.signal(signal.SIGALRM, old_handler) - signal.alarm(0) + self._data = {} self._global_done = None