Multiple processes and recordings

A common need is to log data from multiple processes and then visualize all of that data as part of a single shared recording.

Rerun identifies a recording by its application ID and recording ID. Any data that shares the same application ID and the same recording ID is shown as one shared recording. If either of them differs, you get two separate recordings.

The data can be logged from any number of processes, whether they run on the same machine or not, or implemented in different programming languages. All that matters is that they use the same application ID and the same recording ID.

By default, Rerun generates a random recording ID every time you start a new logging session, but you can override that behavior, e.g.:

rr.init("rerun_example_shared_recording", recording_id="my_shared_recording")

It's up to you to decide where each recording ends up:

  • all processes could stream their share of the data in real-time to a Rerun Viewer,
  • or maybe they all write to their own file on disk that are later loaded in a viewer,
  • or some other combination of the above.

Here's a simple example of such a workflow:

# Process 1 logs some spheres to a recording file.
./app1.py  # rr.init("rerun_example_shared", recording_id="my_shared_recording"); rr.save("/tmp/recording1.rrd")

# Process 2 logs some cubes to another recording file.
./app2.py  # rr.init("rerun_example_shared", recording_id="my_shared_recording"); rr.save("/tmp/recording2.rrd")

# Visualize a 3D scene with both spheres and cubes.
rerun /tmp/recording*.rrd  # they share the same application ID and recording ID!

For more information, check out our dedicated examples:

Multiple threads

A RecordingStream can be shared between threads. In Python, call rr.init once in the main thread, then call rr.log from any thread: all threads log to the same global recording. See the multithreading example.

If different threads should log to different recordings, give each thread its own stream with rr.set_thread_local_data_recording, or use the stream as a context manager (with rec: …). See Log to several recordings from one process.

Multiple Python processes (multiprocessing)

Each process must call rr.init itself, with the same application ID, and then connect to the Viewer. Processes started with Python's multiprocessing inherit the default recording ID of the parent process. If you set recording_id yourself in the parent, pass the same recording_id in every child.

"""Log to one recording from several Python processes."""

import multiprocessing

import rerun as rr


# Forked processes skip the `atexit` handler that flushes data.
@rr.shutdown_at_exit
def task(index: int) -> None:
    # Same application ID as the parent.
    rr.init("rerun_example_multiprocessing_logging")
    rr.connect_grpc()
    rr.log(f"task_{index}", rr.TextLog(f"hello from task {index}"))


if __name__ == "__main__":
    rr.init("rerun_example_multiprocessing_logging")
    # Start a Viewer that the child processes connect to.
    rr.spawn(connect=False)

    for i in range(3):
        p = multiprocessing.Process(target=task, args=(i,))
        p.start()
        p.join()

See the multiprocess logging example.

Combining recording files with the Rerun CLI

To combine several .rrd files into one file, use the Rerun CLI.

rerun rrd merge writes all inputs into one file, but keeps each recording separate: data is only combined if it already has the same application ID and recording ID.

rerun rrd merge -o merged.rrd first.rrd second.rrd

To force several files into one recording, use rerun rrd route to set the application ID and recording ID of all the data:

rerun rrd route --application-id my_app --recording-id my_recording -o combined.rrd first.rrd second.rrd

The Rerun CLI offers several options to manipulate recordings in different ways, check out the CLI reference for more information.

Log to several recordings from one process

rr.init creates one global recording. Every call to rr.init replaces that global recording, so you cannot use it to log to two recordings at the same time.

To log to several recordings from one process, create one RecordingStream per recording.

Create one RecordingStream per recording

Each RecordingStream has its own application ID, recording ID, and sinks. Give each stream a distinct recording ID, otherwise their data ends up in the same recording.

"""Log to two recordings from one process."""

import uuid

import rerun as rr

rec_a = rr.RecordingStream(
    "rerun_example_multiple_recordings", recording_id=uuid.uuid4()
)
rec_b = rr.RecordingStream(
    "rerun_example_multiple_recordings", recording_id=uuid.uuid4()
)

rec_a.spawn()
rec_b.connect_grpc()  # The Viewer started by `rec_a.spawn()` is now running.

rec_a.log("points", rr.Points3D([[0, 0, 0], [1, 1, 1]]))
rec_b.log("points", rr.Points3D([[2, 2, 2], [3, 3, 3]]))

There are three ways to choose which recording a call goes to:

  • Call the method on the stream: rec_a.log(…), rec_a.set_time(…), rec_a.save(…), and so on. Every function that takes an optional recording= argument is also a method on RecordingStream.

  • Pass the stream explicitly: rr.log("points", rr.Points3D(…), recording=rec_a).

  • Use the stream as a context manager. Inside the with block, rr.log and the other module-level functions use that stream on the current thread:

    with rec_a:
        rr.log("points", rr.Points3D([[0, 0, 0]]))

Change the default recording

The module-level functions such as rr.log look up the active recording in this order: the thread-local recording first, then the global recording.

  • rr.set_global_data_recording(rec) makes rec the default for all threads.
  • rr.set_thread_local_data_recording(rec) makes rec the default for the current thread only. Use this to give each worker thread its own recording.

Both functions return the recording they replaced. You can also pass make_default=True or make_thread_default=True to the RecordingStream constructor.

Rust and C++

In Rust, every RecordingStream is already explicit: create one per recording with rerun::RecordingStreamBuilder::new(app_id).recording_id(…).

In C++, create one rerun::RecordingStream(app_id, recording_id) per recording. Use set_global() or set_thread_local() to make one of them the default.

Examples

See also: