Skip to content

Graph Utilities API

github-actions[bot] edited this page Nov 29, 2026 · 2 revisions

The main utility functions of CSP are documented here. These functions enable users to execute and visualize graphs.

Table of Contents

csp.run

csp.run(
    g,
    *args,
    starttime=None,
    endtime=MAX_END_TIME,
    realtime=False,
    output_numpy=False,
    **kwargs,
):

Runs the graph g from starttime to endtime.

Args:

  • g: a graph function or CSP node.
  • args: any positional arguments passed to g.
  • starttime: the engine time that the graph will start at.
  • endtime: the engine time that the graph will end at. Can be a datetime or a timedelta. If a timedelta, the end time is treated as an offset from the start time.
  • realtime: whether the graph runs in realtime versus historical mode. Defaults to historical.
  • output_numpy: if True, will return each output of g as a two element tuple of Numpy arrays, one for the times and one for the values.
  • kwargs: any keyword arguments passed to g.

Returns the outputs of g in a dictionary where the key is the output name and the value is a list of tuples. Each tuple has two elements, the time of the event and its value.

csp.run_on_thread

def run_on_thread(
    g,
    *args,
    starttime=None,
    endtime=MAX_END_TIME,
    queue_wait_time=None,
    realtime=False,
    auto_shutdown=False,
    daemon=False,
    **kwargs,
):

Runs the graph g on a separate thread.

Args:

See csp.run for overlapping arguments.

  • auto_shutdown: if True, the graph will automatically stop and join when the runner goes out scope. Defaults to False.
  • daemon: if True, will launch the thread as a daemon. Defaults to False.

Returns a runner object, which has the following methods:

    def join(self, suppress=False):
        """wait for engine thread to finish and return results. If suppress=True, will suppress exceptions."""

    def is_alive(self):
        """Checks whether the thread is still running"""

    def stop_engine(self):
        """request engine to stop ( async )"""

csp.is_configured_realtime

csp.is_configured_realtime()

Returns the realtime flag passed to csp.run, csp.run_on_thread, or csp.build_graph (default False). Takes no arguments and raises RuntimeError outside a graph run/build context.

During graph construction, this flag can select historical or live input adapters. It does not report the engine's current phase: a realtime-configured run can replay history first. Inside a node, csp.in_realtime() reports that phase. See Execution Modes.

This example uses constants in place of adapters:

from datetime import datetime, timedelta

import csp


def graph():
    if csp.is_configured_realtime():
        return csp.const("live")
    return csp.const("historical")


start = datetime(2024, 1, 1)
for realtime, expected in ((False, "historical"), (True, "live")):
    result = csp.run(graph, starttime=start, endtime=timedelta(0), realtime=realtime)
    assert result[0] == [(start, expected)]

Both runs end at the historical start time. The second selects "live" because its configured mode is realtime, even though no realtime processing occurs.

csp.show_graph

def show_graph(
  graph_func, 
  *args, 
  graph_filename=None, 
  **kwargs,
):

Displays the graph using graphviz.

Args:

  • graph_func: a graph function or CSP node.
  • args: any positional arguments passed to graph_func.
  • graph_filename: a file path to save the image. Default format is png but jpg and others are also supported.
  • kwargs: any keyword arguments passed to graph_func.

Returns None.

Clone this wiki locally