|
22 | 22 | import os |
23 | 23 | from tensorforce import TensorForceError |
24 | 24 | import logging |
| 25 | +import time |
25 | 26 |
|
26 | 27 |
|
27 | 28 | class RemoteEnvironment(Environment): |
@@ -54,20 +55,34 @@ def close(self): |
54 | 55 | """ |
55 | 56 | self.disconnect() |
56 | 57 |
|
57 | | - def connect(self): |
| 58 | + def connect(self, timeout=600): |
58 | 59 | """ |
59 | 60 | Starts the server tcp connection on the given host:port. |
| 61 | +
|
| 62 | + Args: |
| 63 | + timeout (int): The time (in seconds) for which we will attempt a connection to the remote |
| 64 | + (every 5sec). After that (or if timeout is None or 0), an error is raised. |
60 | 65 | """ |
61 | 66 | # If we are already connected, return error. |
62 | 67 | if self.socket: |
63 | 68 | raise TensorForceError("Already connected to {}:{}. Only one connection allowed at a time. " + |
64 | 69 | "Close first by calling `close`!".format(self.host, self.port)) |
65 | 70 | self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
66 | | - self.socket.settimeout(5) |
67 | | - err = self.socket.connect_ex((self.host, self.port)) |
| 71 | + |
| 72 | + if timeout < 5 or timeout is None: |
| 73 | + timeout = 5 |
| 74 | + |
| 75 | + err = 0 |
| 76 | + start_time = time.time() |
| 77 | + while time.time() - start_time < timeout: |
| 78 | + self.socket.settimeout(5) |
| 79 | + err = self.socket.connect_ex((self.host, self.port)) |
| 80 | + if err == 0: |
| 81 | + break |
| 82 | + time.sleep(1) |
68 | 83 | if err != 0: |
69 | | - raise TensorForceError("Error when trying to connect to {}:{}: errno={} '{}'". |
70 | | - format(self.host, self.port, errno.errorcode[err], os.strerror(err))) |
| 84 | + raise TensorForceError("Error when trying to connect to {}:{}: errno={} errcode='{}' '{}'". |
| 85 | + format(self.host, self.port, err, errno.errorcode[err], os.strerror(err))) |
71 | 86 |
|
72 | 87 | def disconnect(self): |
73 | 88 | """ |
|
0 commit comments