Skip to content

Commit

Permalink
Use requests.Session object to reuse connections and use resource mor…
Browse files Browse the repository at this point in the history
…e efficiently in PolicyClient

Signed-off-by: mattias <[email protected]>
  • Loading branch information
MattiasDC committed May 11, 2023
1 parent 707b6ac commit e3586db
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions rllib/env/policy_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ class PolicyClient:

@PublicAPI
def __init__(
self, address: str, inference_mode: str = "local", update_interval: float = 10.0
self,
address: str,
inference_mode: str = "local",
update_interval: float = 10.0,
session: Optional[requests.Session] = None,
):
"""Create a PolicyClient instance.
Expand All @@ -71,8 +75,11 @@ def __init__(
update_interval (float or None): If using 'local' inference mode,
the policy is refreshed after this many seconds have passed,
or None for manual control via client.
session (requests.Session or None): If available the session object
is used to communicate with the policy server.
"""
self.address = address
self.session = session
self.env: ExternalEnv = None
if inference_mode == "local":
self.local = True
Expand Down Expand Up @@ -241,7 +248,12 @@ def update_policy_weights(self) -> None:

def _send(self, data):
payload = pickle.dumps(data)
response = requests.post(self.address, data=payload)

if self.session is None:
response = requests.post(self.address, data=payload)
else:
response = self.session.post(self.address, data=payload)

if response.status_code != 200:
logger.error("Request failed {}: {}".format(response.text, data))
response.raise_for_status()
Expand Down

0 comments on commit e3586db

Please sign in to comment.