Skip to content

Commit 48f55ba

Browse files
sven1977michaelschaarschmidt
authored andcommitted
Add timeout (sec) parameter to RemoteEnvironment.connect().
1 parent 355bfd2 commit 48f55ba

2 files changed

Lines changed: 22 additions & 7 deletions

File tree

tensorforce/contrib/remote_environment.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import os
2323
from tensorforce import TensorForceError
2424
import logging
25+
import time
2526

2627

2728
class RemoteEnvironment(Environment):
@@ -54,20 +55,34 @@ def close(self):
5455
"""
5556
self.disconnect()
5657

57-
def connect(self):
58+
def connect(self, timeout=600):
5859
"""
5960
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.
6065
"""
6166
# If we are already connected, return error.
6267
if self.socket:
6368
raise TensorForceError("Already connected to {}:{}. Only one connection allowed at a time. " +
6469
"Close first by calling `close`!".format(self.host, self.port))
6570
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)
6883
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)))
7186

7287
def disconnect(self):
7388
"""

tensorforce/contrib/unreal_engine.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ def __init__(
5050
This would be necessary e.g. for agents that use q-networks where the output are q-values per discrete
5151
state-action pair.
5252
delta_time (float): The fake delta time to use for each single game tick.
53-
num_ticks (int): The number of ticks to be executed in this step (each tick will repeat the same given
54-
actions).
53+
num_ticks (int): The number of ticks to be executed in a single act call (each tick will
54+
repeat the same given actions).
5555
"""
5656
RemoteEnvironment.__init__(self, host, port)
5757

0 commit comments

Comments
 (0)