|
7 | 7 | import os |
8 | 8 | import socket |
9 | 9 | import subprocess |
10 | | -import signal |
11 | 10 |
|
12 | 11 | from .brain import BrainInfo, BrainParameters |
13 | 12 | from .exception import UnityEnvironmentException, UnityActionException |
@@ -76,28 +75,27 @@ def __init__(self, file_name, worker_id=0, |
76 | 75 | except os.error: |
77 | 76 | self.close() |
78 | 77 | raise UnityEnvironmentException("Couldn't launch new environment. " |
79 | | - "Provided filename does not match any \environments in {}." |
80 | | - .format(cwd)) |
| 78 | + "Provided filename does not match any \environments in {}." |
| 79 | + .format(cwd)) |
81 | 80 |
|
82 | | - def timeout_handler(): |
83 | | - raise UnityEnvironmentException( |
| 81 | + self._socket.settimeout(30) |
| 82 | + try: |
| 83 | + try: |
| 84 | + self._socket.listen(1) |
| 85 | + self._conn, _ = self._socket.accept() |
| 86 | + self._conn.setblocking(1) |
| 87 | + p = self._conn.recv(self._buffer_size).decode('utf-8') |
| 88 | + p = json.loads(p) |
| 89 | + except socket.timeout as e: |
| 90 | + raise UnityEnvironmentException( |
84 | 91 | "The Unity environment took too long to respond. Make sure {} does not need user interaction to launch " |
85 | | - "and that the Academy and the external Brain(s) scripts are attached to objects in the Scene.".format( |
| 92 | + "and that the Academy and the external Brain(s) are attached to objects in the Scene.".format( |
86 | 93 | str(file_name))) |
87 | | - |
88 | | - old_handler = signal.signal(signal.SIGALRM, timeout_handler) |
89 | | - signal.alarm(30) # trigger alarm in x seconds |
90 | | - try: |
91 | | - self._socket.listen(1) |
92 | | - self._conn, _ = self._socket.accept() |
93 | | - p = self._conn.recv(self._buffer_size).decode('utf-8') |
94 | | - p = json.loads(p) |
95 | 94 | except UnityEnvironmentException: |
96 | 95 | proc1.kill() |
97 | 96 | self.close() |
98 | 97 | raise |
99 | | - signal.signal(signal.SIGALRM, old_handler) |
100 | | - signal.alarm(0) |
| 98 | + |
101 | 99 |
|
102 | 100 | self._data = {} |
103 | 101 | self._global_done = None |
|
0 commit comments