-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--- | ||
id: overview_interactive_processes | ||
title: "Interactive Processes" | ||
--- | ||
|
||
Sometimes you want to interact with a process in a back-and-forth manner by sending requests to the process and receiving responses back. For example, interacting with a repl-like process like `node -i`, `python -i`, etc. or an ssh server. | ||
|
||
Here is an example of communicating with an interactive Python shell: | ||
|
||
```scala mdoc:invisible | ||
import zio._ | ||
import zio.process._ | ||
import java.nio.charset.StandardCharsets | ||
``` | ||
|
||
```scala mdoc:silent | ||
for { | ||
commandQueue <- Queue.unbounded[Chunk[Byte]] | ||
process <- Command("python", "-qi").stdin(ProcessInput.fromQueue(commandQueue)).run | ||
_ <- process.stdout.linesStream.foreach { response => | ||
ZIO.debug(s"Response from REPL: $response") | ||
}.forkDaemon | ||
_ <- commandQueue.offer(Chunk.fromArray("1+1\n".getBytes(StandardCharsets.UTF_8))) | ||
_ <- commandQueue.offer(Chunk.fromArray("2**8\n".getBytes(StandardCharsets.UTF_8))) | ||
_ <- commandQueue.offer(Chunk.fromArray("import random\nrandom.randint(1, 100)\n".getBytes(StandardCharsets.UTF_8))) | ||
_ <- commandQueue.offer(Chunk.fromArray("exit()\n".getBytes(StandardCharsets.UTF_8))) | ||
} yield () | ||
``` | ||
|
||
You would probably want to create a helper for the repeated code, but this just a minimal example to help get you started. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters