解决Carla同步模式下Runtime Error的问题(RuntimeError: time-out of 10000ms while waiting for the simulator, make sure the simulator is ready and connected to localhost:2000)
问题描述
Carla仿真平台下,使用同步模式更新服务器进行渲染,在运行一段时间后可能会在world.tick( )处报错无法连接simulator,出现Runtime Error导致程序终止。
该错误出现版本从8到9,从Ubuntu到Windows,是普遍且严重的Bug。
Synchronous mode
Traceback (most recent call last):
File “tick_hang.py”, line 17, in
world.tick()
RuntimeError: time-out of 10000ms while waiting for the simulator, make sure the simulator is ready and connected to localhost:2000
更多类似问题可见GitHub issue 链接
Synchronous mode - tick hangs #2809
出现该问题的源头
Carla采用的是客户端-服务器架构,client发送tick请求更新,并接收server的渲染成功的信号。
当client接收server的信号失败后,将会等待一段时间,最终报错Runtime Error,这种情况大多数是因为Server的返回信号丢失,而并不是没有渲染,client未接收到返回信号会默认服务器已不存在,从而产生该错误。
解决方案
参考上述issue链接,会发现并官方没有完美的解决的方案,我的思路是,使用python的异常处理语句忽略掉该异常。
给每一次的tick( )语句,添加try语句进行异常处理。
try:
self.world.tick( )
expect:
print("tick 丢失,重新发送")
time.sleep(2)
self.world.tick( )
若要保证你的代码不会停止,或许再加一层While True循环也可以
While True:
try:
self.world.tick()
break
expect:
print("tick 丢失,重新发送")
time.sleep(2)
在我的情形下,该问题得到了解决!