Skip to content

Commit

Permalink
Adds information to redirect_std* inline docs (#49563)
Browse files Browse the repository at this point in the history
Partially addresses issue #35959

Adds examples for redirecting std to a file to the markdown file

---------

Co-authored-by: Jameson Nash <[email protected]>
Co-authored-by: Steven G. Johnson <[email protected]>
  • Loading branch information
3 people authored Oct 31, 2023
1 parent b9dcb94 commit 6084a62
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions doc/src/manual/networking-and-streams.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Networking and Streams

Julia provides a rich interface to deal with streaming I/O objects such as terminals, pipes and
TCP sockets. This interface, though asynchronous at the system level, is presented in a synchronous
manner to the programmer and it is usually unnecessary to think about the underlying asynchronous
operation. This is achieved by making heavy use of Julia cooperative threading ([coroutine](@ref man-tasks))
TCP sockets.
These objects allow data to be sent and received in a stream-like fashion, which means that data is processed sequentially as it becomes available.
This interface, though asynchronous at the system level, is presented in a synchronous manner to the programmer.
This is achieved by making heavy use of Julia cooperative threading ([coroutine](@ref man-tasks))
functionality.

## Basic Stream I/O
Expand Down Expand Up @@ -66,8 +67,8 @@ abcd
"abcd"
```

Note that depending on your terminal settings, your TTY may be line buffered and might thus require
an additional enter before the data is sent to Julia.
Note that depending on your terminal settings, your TTY ("teletype terminal") may be line buffered and might thus require an additional enter before `stdin` data is sent to Julia.
When running Julia from the command line in a TTY, output is sent to the console by default, and standard input is read from the keyboard.

To read every line from [`stdin`](@ref) you can use [`eachline`](@ref):

Expand Down Expand Up @@ -205,6 +206,24 @@ julia> open("hello.txt") do f
"HELLO AGAIN."
```

If you want to redirect stdout to a file

```# Open file for writing
out_file = open("output.txt", "w")
# Redirect stdout to file
redirect_stdout(out_file) do
# Your code here
println("This output goes to `out_file` via the `stdout` variable.")
end
# Close file
close(out_file)
```

Redirecting stdout to a file can help you save and analyze program output, automate processes, and meet compliance requirements.

## A simple TCP example

Let's jump right in with a simple example involving TCP sockets.
Expand Down Expand Up @@ -336,7 +355,6 @@ ip"74.125.226.225"

## Asynchronous I/O


All I/O operations exposed by [`Base.read`](@ref) and [`Base.write`](@ref) can be performed
asynchronously through the use of [coroutines](@ref man-tasks). You can create a new coroutine to
read from or write to a stream using the [`@async`](@ref) macro:
Expand Down Expand Up @@ -418,6 +436,7 @@ close(socket)
This example gives the same functionality as the previous program, but uses IPv6 as the network-layer protocol.

Listener:

```julia
using Sockets
group = Sockets.IPv6("ff05::5:6:7")
Expand All @@ -430,6 +449,7 @@ close(socket)
```

Sender:

```julia
using Sockets
group = Sockets.IPv6("ff05::5:6:7")
Expand Down

0 comments on commit 6084a62

Please sign in to comment.