@@ -71,6 +71,8 @@ def __init__(self, server_url: str):
7171 self ._agent_jet_job = None
7272 # throttle
7373 self ._recent_seen_tasks = []
74+ # reuse httpx client to avoid creating SSL context repeatedly
75+ self ._http_client = httpx .Client (timeout = GENERAL_TIMEOUT )
7476
7577 def logger_info (self , message ):
7678 # logger with de-duplication within 1 second to prevent log flooding
@@ -252,10 +254,9 @@ def _begin_episode_auto_retry(self, discard_episode_timeout=240, episode_type="t
252254 discard_episode_timeout = discard_episode_timeout ,
253255 throttle_policy = throttle_policy
254256 )
255- resp = httpx .post (
257+ resp = self . _http_client .post (
256258 f"{ self .server_url } /claim_episode" ,
257- json = req_obj .model_dump (),
258- timeout = GENERAL_TIMEOUT
259+ json = req_obj .model_dump ()
259260 )
260261 raise_for_status_with_detail (resp )
261262 data = ClaimEpisodeResponse .model_validate (resp .json ())
@@ -337,10 +338,9 @@ def end_episode(self, task:Task, episode_uuid: str, workflow_output: WorkflowOut
337338 task_id = task_id
338339 )
339340
340- resp = httpx .post (
341+ resp = self . _http_client .post (
341342 f"{ self .server_url } /end_episode" ,
342- json = req_obj .model_dump (),
343- timeout = GENERAL_TIMEOUT
343+ json = req_obj .model_dump ()
344344 )
345345 raise_for_status_with_detail (resp )
346346 data = EndEpisodeResponse .model_validate (resp .json ())
@@ -366,10 +366,9 @@ def abort_episode(self, episode_uuid: str):
366366 task_id = ""
367367 )
368368
369- resp = httpx .post (
369+ resp = self . _http_client .post (
370370 f"{ self .server_url } /abort_episode" ,
371- json = req_obj .model_dump (),
372- timeout = GENERAL_TIMEOUT
371+ json = req_obj .model_dump ()
373372 )
374373 raise_for_status_with_detail (resp )
375374 data = EndEpisodeResponse .model_validate (resp .json ())
@@ -399,10 +398,9 @@ def sync_train_config(self, agent_jet_job: AgentJetJob):
399398
400399 req_obj = SyncTrainConfigRequest (yaml_as_string = yaml_str )
401400
402- resp = httpx .post (
401+ resp = self . _http_client .post (
403402 f"{ self .server_url } /sync_train_config" ,
404- json = req_obj .model_dump (),
405- timeout = GENERAL_TIMEOUT
403+ json = req_obj .model_dump ()
406404 )
407405 raise_for_status_with_detail (resp )
408406 self .logger_info ("Synced train config to Swarm server" )
@@ -422,7 +420,7 @@ def start_engine(self):
422420 raise RuntimeError (f"Cannot start engine when engine is NOT ENGINE.OFFLINE. (current status: { current_status } )" )
423421
424422 # Send start engine request
425- resp = httpx .post (
423+ resp = self . _http_client .post (
426424 f"{ self .server_url } /start_engine" ,
427425 json = {},
428426 timeout = 600
@@ -487,7 +485,7 @@ def _wait_until_status_change_to(self, desired_status="ENGINE.ROLLING", verbose=
487485 @cache_with_ttl (ttl = 0.5 )
488486 def get_engine_status (self ) -> Tuple [str , dict ]:
489487 try :
490- resp = httpx .get (
488+ resp = self . _http_client .get (
491489 f"{ self .server_url } /get_engine_status" ,
492490 timeout = 10
493491 )
@@ -512,7 +510,7 @@ def can_continue_episode(self, episode_uuid: str) -> bool:
512510 client_uuid = self .client_uuid ,
513511 episode_uuid = episode_uuid
514512 )
515- resp = httpx .post (
513+ resp = self . _http_client .post (
516514 f"{ self .server_url } /can_continue_episode" ,
517515 json = req_obj .model_dump (),
518516 timeout = 10
@@ -526,7 +524,7 @@ def can_continue_episode(self, episode_uuid: str) -> bool:
526524
527525 def get_episode_buffer (self ) -> List [EpisodeStatus ]:
528526 try :
529- resp = httpx .post (
527+ resp = self . _http_client .post (
530528 f"{ self .server_url } /get_episode_buffer" ,
531529 json = {},
532530 timeout = 10
@@ -585,7 +583,7 @@ def stop_engine(self):
585583 self .logger_info ("Engine is already OFFLINE. No action needed." )
586584 return
587585
588- resp = httpx .post (
586+ resp = self . _http_client .post (
589587 f"{ self .server_url } /stop_engine" ,
590588 json = {},
591589 timeout = 600
@@ -605,7 +603,7 @@ def get_rollout_stat(self) -> CurrentBatchRolloutPoolInformation:
605603 Returns statistics about completed episodes, tasks, and progress.
606604 """
607605 try :
608- resp = httpx .get (
606+ resp = self . _http_client .get (
609607 f"{ self .server_url } /get_current_batch_rollout_pool_information" ,
610608 timeout = 10
611609 )
0 commit comments