Skip to content

Commit 04d37fb

Browse files
committed
Document how to use job send and job recv
1 parent edb82d0 commit 04d37fb

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

book/background_jobs.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,35 @@ job unfreeze 1
9191
# we're back in vim
9292
```
9393

94+
## Communicating between jobs
95+
96+
Data can be sent to a job using `job send <id>`, and the job can receive it using `job recv`:
97+
98+
```nu
99+
let jobId = job spawn {
100+
job recv | save sent.txt
101+
}
102+
103+
'hello from the main thread' | job send $jobId
104+
105+
sleep 1sec
106+
107+
open sent.txt
108+
# => hello from the main thread
109+
```
110+
111+
The main thread has a job ID of 0, so you can also send data in the other direction:
112+
113+
```nu
114+
job spawn {
115+
sleep 1sec
116+
'Hello from a background job' | job send 0
117+
}
118+
119+
job recv
120+
# => Hello from a background job
121+
```
122+
94123
## Exit Behavior
95124

96125
Unlike many other shells, Nushell jobs are **not** separate processes,

0 commit comments

Comments
 (0)