Skip to content

Feat/mujoco model #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion demos/fancy_gym/BoxPushingDense.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
env = fancy_gym.make(env_name, seed=1)
obs = env.reset()

publisher = FancyGymPublisher(env_name, env, "127.0.0.1")
publisher = FancyGymPublisher(env, "127.0.0.1")

for i in range(1000):
action = env.action_space.sample()
Expand Down
19 changes: 19 additions & 0 deletions demos/fancy_gym/TableTennis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import fancy_gym
import time

from simpub.sim.fancy_gym import FancyGymPublisher

env_name = "TableTennis2D-v0"

env = fancy_gym.make(env_name, seed=1)
obs = env.reset()

publisher = FancyGymPublisher(env, "127.0.0.1")

while True:
action = env.action_space.sample()
obs, reward, done, info = env.step(action)
env.render()
time.sleep(0.01)
if done:
obs = env.reset()
File renamed without changes.
37 changes: 16 additions & 21 deletions simpub/core/net_manager.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import concurrent.futures
import enum
from typing import List, Dict
from typing import NewType, Callable, TypedDict, Union
import asyncio
from asyncio import sleep as asycnc_sleep
import threading
import zmq
import zmq.asyncio
import socket
from socket import AF_INET, SOCK_DGRAM, SOL_SOCKET, SO_BROADCAST
import struct
import time
import json
from json import dumps
import uuid
from .log import logger
from json import dumps
import zmq
import abc
import concurrent

IPAddress = NewType("IPAddress", str)
TopicName = NewType("TopicName", str)
Expand Down Expand Up @@ -140,8 +140,10 @@ def __init__(

async def callback(self, msg: str):
result = await asyncio.wait_for(
asyncio.to_thread(self.callback_func, msg),
timeout=5
self.manager.loop.run_in_executor(
self.manager.executor, self.callback_func, msg
),
timeout=5.0,
)
await self.sender(result)

Expand Down Expand Up @@ -190,19 +192,9 @@ def __init__(
# setting up thread pool
self.running: bool = True
self.loop: asyncio.AbstractEventLoop = None
self.start_server_thread()

def start_server_thread(self) -> None:
"""
Start a thread for service.

Args:
block (bool, optional): main thread stop running and
wait for server thread. Defaults to False.
"""
self.server_thread = threading.Thread(target=self.start_event_loop)
self.server_thread.daemon = True
self.server_thread.start()
# start the server in a thread pool
self.executor = concurrent.futures.ThreadPoolExecutor(max_workers=5)
self.server_future = self.executor.submit(self.start_event_loop)
while self.loop is None:
time.sleep(0.01)

Expand Down Expand Up @@ -233,10 +225,10 @@ def submit_task(self, task: Callable, *args) -> asyncio.Future:
def stop_server(self):
if self.loop.is_running():
asyncio.run_coroutine_threadsafe(self.loop.stop(), self.loop)
self.server_thread.join()
self.executor.shutdown(wait=True)

def join(self):
self.server_thread.join()
self.executor.shutdown(wait=True)

async def service_loop(self):
# try:
Expand All @@ -258,7 +250,10 @@ async def service_loop(self):
)
await self.service_socket.send_string("Timeout")
except Exception as e:
logger.error(f"Error: {e}")
logger.error(
f"One error ocurred when processing the Service "
f"\"{service}\": {e}"
)
await asycnc_sleep(0.01)

async def broadcast_loop(self):
Expand Down
Loading