From 4958f041a18fb6c55e2827245ff5fc4eaeabfb8c Mon Sep 17 00:00:00 2001 From: vincentpierre Date: Wed, 20 Sep 2017 14:50:51 -0700 Subject: [PATCH 1/2] fix on the socket timeout error on windows due to the use of signal.SIGALRM --- python/unityagents/environment.py | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/python/unityagents/environment.py b/python/unityagents/environment.py index 22349ce021..3a36320867 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.settimeout(30) + 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 From 23a3514944285a9de87aa1e2d2b86cf7e80ff5ae Mon Sep 17 00:00:00 2001 From: vincentpierre Date: Wed, 20 Sep 2017 17:06:43 -0700 Subject: [PATCH 2/2] changed the connection to non-blocking --- python/unityagents/environment.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/unityagents/environment.py b/python/unityagents/environment.py index 3a36320867..2f39afa71c 100755 --- a/python/unityagents/environment.py +++ b/python/unityagents/environment.py @@ -83,7 +83,7 @@ def __init__(self, file_name, worker_id=0, try: self._socket.listen(1) self._conn, _ = self._socket.accept() - self._conn.settimeout(30) + self._conn.setblocking(1) p = self._conn.recv(self._buffer_size).decode('utf-8') p = json.loads(p) except socket.timeout as e: