Make SimulatorBridge picklable (MacOS support) (#34023)

* remove unused

* make Params (un)serializable

* move initialization after spawn

_thread.lock objects can't be pickled
This commit is contained in:
JaSpa99 2024-11-14 19:25:50 +01:00 committed by GitHub
parent 50aac48fba
commit dc73e6e2aa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 3 deletions

View File

@ -36,11 +36,16 @@ class UnknownKeyName(Exception):
cdef class Params: cdef class Params:
cdef c_Params* p cdef c_Params* p
cdef str d
def __cinit__(self, d=""): def __cinit__(self, d=""):
cdef string path = <string>d.encode() cdef string path = <string>d.encode()
with nogil: with nogil:
self.p = new c_Params(path) self.p = new c_Params(path)
self.d = d
def __reduce__(self):
return (type(self), (self.d,))
def __dealloc__(self): def __dealloc__(self):
del self.p del self.p

View File

@ -47,12 +47,11 @@ class SimulatorBridge(ABC):
self.dual_camera = dual_camera self.dual_camera = dual_camera
self.high_quality = high_quality self.high_quality = high_quality
self._exit_event = threading.Event() self._exit_event: threading.Event | None = None
self._threads = [] self._threads = []
self._keep_alive = True self._keep_alive = True
self.started = Value('i', False) self.started = Value('i', False)
signal.signal(signal.SIGTERM, self._on_shutdown) signal.signal(signal.SIGTERM, self._on_shutdown)
self._exit = threading.Event()
self.simulator_state = SimulatorState() self.simulator_state = SimulatorState()
self.world: World | None = None self.world: World | None = None
@ -76,7 +75,9 @@ class SimulatorBridge(ABC):
def close(self, reason): def close(self, reason):
self.started.value = False self.started.value = False
self._exit_event.set()
if self._exit_event is not None:
self._exit_event.set()
if self.world is not None: if self.world is not None:
self.world.close(reason) self.world.close(reason)
@ -103,6 +104,8 @@ Ignition: {self.simulator_state.ignition} Engaged: {self.simulator_state.is_enga
self.simulated_car = SimulatedCar() self.simulated_car = SimulatedCar()
self.simulated_sensors = SimulatedSensors(self.dual_camera) self.simulated_sensors = SimulatedSensors(self.dual_camera)
self._exit_event = threading.Event()
self.simulated_car_thread = threading.Thread(target=rk_loop, args=(functools.partial(self.simulated_car.update, self.simulator_state), self.simulated_car_thread = threading.Thread(target=rk_loop, args=(functools.partial(self.simulated_car.update, self.simulator_state),
100, self._exit_event)) 100, self._exit_event))
self.simulated_car_thread.start() self.simulated_car_thread.start()