mirror of
https://github.com/huggingface/lerobot.git
synced 2026-07-23 01:41:54 +00:00
use main thread to run cmd_forward_loop, close threads upon shutdown_event
This commit is contained in:
@@ -99,11 +99,12 @@ def state_forward_loop(
|
|||||||
lowstate_sub: ChannelSubscriber,
|
lowstate_sub: ChannelSubscriber,
|
||||||
lowstate_sock: zmq.Socket,
|
lowstate_sock: zmq.Socket,
|
||||||
state_period: float,
|
state_period: float,
|
||||||
|
shutdown_event: threading.Event,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Read observation from DDS and forward to ZMQ clients."""
|
"""Read observation from DDS and forward to ZMQ clients."""
|
||||||
last_state_time = 0.0
|
last_state_time = 0.0
|
||||||
|
|
||||||
while True:
|
while not shutdown_event.is_set():
|
||||||
# read from DDS
|
# read from DDS
|
||||||
msg = lowstate_sub.Read()
|
msg = lowstate_sub.Read()
|
||||||
if msg is None:
|
if msg is None:
|
||||||
@@ -128,7 +129,10 @@ def cmd_forward_loop(
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Receive commands from ZMQ and forward to DDS."""
|
"""Receive commands from ZMQ and forward to DDS."""
|
||||||
while True:
|
while True:
|
||||||
payload = lowcmd_sock.recv()
|
try:
|
||||||
|
payload = lowcmd_sock.recv()
|
||||||
|
except zmq.ContextTerminated:
|
||||||
|
break
|
||||||
msg_dict = json.loads(payload.decode("utf-8"))
|
msg_dict = json.loads(payload.decode("utf-8"))
|
||||||
|
|
||||||
topic = msg_dict.get("topic", "")
|
topic = msg_dict.get("topic", "")
|
||||||
@@ -182,30 +186,26 @@ def main() -> None:
|
|||||||
lowstate_sock.bind(f"tcp://0.0.0.0:{LOWSTATE_PORT}")
|
lowstate_sock.bind(f"tcp://0.0.0.0:{LOWSTATE_PORT}")
|
||||||
|
|
||||||
state_period = 0.002 # ~500 hz
|
state_period = 0.002 # ~500 hz
|
||||||
|
shutdown_event = threading.Event()
|
||||||
|
|
||||||
# start observation forwarding thread
|
# start observation forwarding in background thread
|
||||||
t_state = threading.Thread(
|
t_state = threading.Thread(
|
||||||
target=state_forward_loop,
|
target=state_forward_loop,
|
||||||
args=(lowstate_sub, lowstate_sock, state_period),
|
args=(lowstate_sub, lowstate_sock, state_period, shutdown_event),
|
||||||
daemon=True,
|
|
||||||
)
|
)
|
||||||
t_state.start()
|
t_state.start()
|
||||||
|
|
||||||
# start action forwarding thread
|
|
||||||
t_cmd = threading.Thread(
|
|
||||||
target=cmd_forward_loop,
|
|
||||||
args=(lowcmd_sock, lowcmd_pub_debug, crc),
|
|
||||||
daemon=True,
|
|
||||||
)
|
|
||||||
t_cmd.start()
|
|
||||||
|
|
||||||
print("bridge running (lowstate -> zmq, lowcmd -> dds)")
|
print("bridge running (lowstate -> zmq, lowcmd -> dds)")
|
||||||
# keep main thread alive so daemon threads don't exit
|
|
||||||
|
# run command forwarding in main thread
|
||||||
try:
|
try:
|
||||||
while True:
|
cmd_forward_loop(lowcmd_sock, lowcmd_pub_debug, crc)
|
||||||
time.sleep(1.0)
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print("shutting down bridge...")
|
print("shutting down bridge...")
|
||||||
|
finally:
|
||||||
|
shutdown_event.set()
|
||||||
|
ctx.term() # terminates blocking zmq.recv() calls
|
||||||
|
t_state.join(timeout=2.0)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Reference in New Issue
Block a user