From 04d37fb4b86de20ba758ed204b94a6472c893fad Mon Sep 17 00:00:00 2001 From: ItsHarper <10224994+ItsHarper@users.noreply.github.com> Date: Wed, 6 Aug 2025 17:44:40 -0500 Subject: [PATCH] Document how to use `job send` and `job recv` --- book/background_jobs.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/book/background_jobs.md b/book/background_jobs.md index 212c8e37bdc..2703532c8e5 100644 --- a/book/background_jobs.md +++ b/book/background_jobs.md @@ -91,6 +91,35 @@ job unfreeze 1 # we're back in vim ``` +## Communicating between jobs + +Data can be sent to a job using `job send `, and the job can receive it using `job recv`: + +```nu +let jobId = job spawn { + job recv | save sent.txt +} + +'hello from the main thread' | job send $jobId + +sleep 1sec + +open sent.txt +# => hello from the main thread +``` + +The main thread has a job ID of 0, so you can also send data in the other direction: + +```nu +job spawn { + sleep 1sec + 'Hello from a background job' | job send 0 +} + +job recv +# => Hello from a background job +``` + ## Exit Behavior Unlike many other shells, Nushell jobs are **not** separate processes,