IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: Nightly
For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).

initialize_runtime

def initialize_runtime()

Initializes the global Mojo runtime if it is not already initialized.

The Mojo runtime manages the thread pool used by parallel and asynchronous APIs such as parallelize() and TaskGroup. Programs with a Mojo main() function initialize the runtime automatically at startup, so most programs never need to call this function.

However, when Mojo code is compiled into a shared library (with mojo build --emit shared-lib) and called from a non-Mojo host program (such as C or C++), no Mojo main() function runs and the runtime is never initialized. In that case, call this function before using any API that depends on the runtime — for example, at the start of each function exported with @export. This function is idempotent and inexpensive when the runtime is already initialized.

Initializing the runtime once covers all threads in the process. The runtime remains alive for the remainder of the process.

Examples:

from max.algorithm import parallelize
from std.runtime import initialize_runtime


@export("fill_squares")
def fill_squares(
data: Pointer[Int64, MutUntrackedOrigin], len: Int
) abi("C"):
initialize_runtime()

@parameter
def fill(i: Int):
data.unsafe_store(i, Int64(i * i))

parallelize[fill](len)