carla-simulator / carla

Open-source simulator for autonomous driving research.
http://carla.org
MIT License
11.32k stars 3.67k forks source link

How to close cleanly the simulator #5575

Open RocaPiedra opened 2 years ago

RocaPiedra commented 2 years ago

CARLA version: 0.9.13 Platform/OS: Linux and Windows (I'm using mostly linux but I want it to be agnostic) Problem you have experienced: Can't automate simulator termination

Hi,

I'm doing an app to interact with the simulator and I haven't found a reliable way to automate the process of shutting down the simulator. What I do is try to connect to the simulator like this:

try:
        client = carla.Client(args.host, args.port)
        client.set_timeout(5.0)
        run_simulation(args, client)

except (RuntimeError, TypeError, NameError):
        roc_functions.launch_carla_simulator_locally()
        client = carla.Client(args.host, args.port)
        client.set_timeout(5.0)
        run_simulation(args, client)

and if it is not reachable I spawn it as a Python subprocess through this function:

def launch_carla_simulator_locally(unreal_engine_path = None):
    if unreal_engine_path is None:
        unreal_engine_path = parameters.unreal_engine_path
    print('Launching Unreal Engine Server...')
    if os.name == 'nt':
        unreal_engine = subprocess.Popen(unreal_engine_path, stdout=subprocess.PIPE)
    else:
        unreal_engine = subprocess.Popen([unreal_engine_path], stdout=subprocess.PIPE)
    sleep(5)
    print('Generating traffic...')
    generate_traffic = subprocess.Popen(["python", "../carlacomms/generate_traffic.py", '--asynch', '--tm-port=8001'], stdout=subprocess.PIPE)
    return unreal_engine, generate_traffic

Then, when the run simulation is finished or an exception is thrown, I terminate the processes like this:

finally:
        print('terminating traffic...')
        generate_traffic.terminate()
        print(generate_traffic.poll())
        print('terminating simulator...')
        unreal_engine.terminate()
        print(unreal_engine.poll())

But the simulator keeps running and I have to close the process manually.

fangchaooo commented 2 years ago

You can kill -9 $ps aux |grep Unreal.

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.