You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Adds information to redirect_std* inline docs (#49563)
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]>
Copy file name to clipboardExpand all lines: doc/src/manual/networking-and-streams.md
+26-6Lines changed: 26 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,10 @@
1
1
# Networking and Streams
2
2
3
3
Julia provides a rich interface to deal with streaming I/O objects such as terminals, pipes and
4
-
TCP sockets. This interface, though asynchronous at the system level, is presented in a synchronous
5
-
manner to the programmer and it is usually unnecessary to think about the underlying asynchronous
6
-
operation. This is achieved by making heavy use of Julia cooperative threading ([coroutine](@ref man-tasks))
4
+
TCP sockets.
5
+
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.
6
+
This interface, though asynchronous at the system level, is presented in a synchronous manner to the programmer.
7
+
This is achieved by making heavy use of Julia cooperative threading ([coroutine](@ref man-tasks))
7
8
functionality.
8
9
9
10
## Basic Stream I/O
@@ -66,8 +67,8 @@ abcd
66
67
"abcd"
67
68
```
68
69
69
-
Note that depending on your terminal settings, your TTY may be line buffered and might thus require
70
-
an additional enter before the data is sent to Julia.
70
+
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.
71
+
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.
71
72
72
73
To read every line from [`stdin`](@ref) you can use [`eachline`](@ref):
73
74
@@ -205,6 +206,24 @@ julia> open("hello.txt") do f
205
206
"HELLO AGAIN."
206
207
```
207
208
209
+
If you want to redirect stdout to a file
210
+
211
+
```# Open file for writing
212
+
out_file = open("output.txt", "w")
213
+
214
+
# Redirect stdout to file
215
+
redirect_stdout(out_file) do
216
+
# Your code here
217
+
println("This output goes to `out_file` via the `stdout` variable.")
218
+
end
219
+
220
+
# Close file
221
+
close(out_file)
222
+
223
+
```
224
+
225
+
Redirecting stdout to a file can help you save and analyze program output, automate processes, and meet compliance requirements.
226
+
208
227
## A simple TCP example
209
228
210
229
Let's jump right in with a simple example involving TCP sockets.
@@ -336,7 +355,6 @@ ip"74.125.226.225"
336
355
337
356
## Asynchronous I/O
338
357
339
-
340
358
All I/O operations exposed by [`Base.read`](@ref) and [`Base.write`](@ref) can be performed
341
359
asynchronously through the use of [coroutines](@ref man-tasks). You can create a new coroutine to
342
360
read from or write to a stream using the [`@async`](@ref) macro:
@@ -418,6 +436,7 @@ close(socket)
418
436
This example gives the same functionality as the previous program, but uses IPv6 as the network-layer protocol.
0 commit comments