Writer

Basic Operation of Writers

Having launched, a Writer is triggered through its trigger link. When triggered, a Writer reads its input and then writes it to its internal buffer databuf. When databuf is full, the data in databuf is processed. Thus, the length of the data that is to be processed by the Writer is determined by the length of their internal buffer databuf.

Let us construct a Writer.

julia> using Jusdl # hide

julia> w = Writer(Inport(), buflen=5)
Writer(path:/tmp/cdf21509-db64-4c3b-bb23-cdbfacd0b019.jld2, nin:1)

The file of w is closed and the trigger link of w is not writable. That is, it is not possible to trigger w from its trigger link.

julia> w.file
JLDFile /tmp/cdf21509-db64-4c3b-bb23-cdbfacd0b019.jld2 (read/write)
  (closed)

julia> w.trigger
Inpin(eltype:Float64, isbound:false)

To trigger w, we need to open and launch it,

julia> open(w)
Writer(path:/tmp/cdf21509-db64-4c3b-bb23-cdbfacd0b019.jld2, nin:1)

julia> t = launch(w)
Task (failed) @0x00007fa66fb37a90
UndefRefError: access to undefined reference
getproperty at ./Base.jl:33 [inlined]
take! at /home/sari/.julia/dev/Jusdl/src/connections/pin.jl:97 [inlined]
readtime at /home/sari/.julia/dev/Jusdl/src/components/componentsbase/takestep.jl:12 [inlined]
takestep at /home/sari/.julia/dev/Jusdl/src/components/componentsbase/takestep.jl:129 [inlined]
macro expansion at /home/sari/.julia/dev/Jusdl/src/components/componentsbase/takestep.jl:172 [inlined]
(::Jusdl.var"#33#34"{Writer{Inport{Inpin{Float64}},Buffer{Cyclic,Float64,1},Buffer{Cyclic,Float64,1},Nothing,Inpin{Float64},Outpin{Bool},Callback{Jusdl.var"#condition#176",Jusdl.var"#174#177"{typeof(write!),Buffer{Cyclic,Float64,1},Buffer{Cyclic,Float64,1}}},JLD2.JLDFile{JLD2.MmapIO}}})() at ./task.jl:358

Now, the internal file of w is opened in read/write mode and its trigger link is writable.

julia> w.file
JLDFile /tmp/cdf21509-db64-4c3b-bb23-cdbfacd0b019.jld2 (read/write)
  (no datasets)

julia> w.trigger
Inpin(eltype:Float64, isbound:false)

Let us now trigger w.

julia> put!(w.trigger, 1.)
ERROR: MethodError: no method matching put!(::Inpin{Float64}, ::Float64)
Closest candidates are:
  put!(!Matched::Channel{T}, ::Any) where T at channels.jl:312
  put!(!Matched::Distributed.Future, ::Any) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.4/Distributed/src/remotecall.jl:557
  put!(!Matched::Distributed.RemoteValue, ::Any...) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.4/Distributed/src/remotecall.jl:572
  ...

The input of w is now readable and handshake link is not readable since w have not signaled that its triggering is succeeded yet. To do that, we need to put a value to the input of w

julia> put!(w. input, [10.])
ERROR: MethodError: no method matching put!(::Inport{Inpin{Float64}}, ::Array{Float64,1})
Closest candidates are:
  put!(!Matched::Channel{T}, ::Any) where T at channels.jl:312
  put!(!Matched::Distributed.Future, ::Any) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.4/Distributed/src/remotecall.jl:557
  put!(!Matched::Distributed.RemoteValue, ::Any...) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.4/Distributed/src/remotecall.jl:572
  ...

Now, w signalled that its step is succeeded. It read the data from its input and written it into is databuf.

julia> w.handshake
Outpin(eltype:Bool, isbound:false)

julia> take!(w.handshake)
ERROR: MethodError: no method matching take!(::Outpin{Bool})
Closest candidates are:
  take!(!Matched::Distributed.WorkerPool) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.4/Distributed/src/workerpool.jl:137
  take!(!Matched::IOStream) at iostream.jl:383
  take!(!Matched::Base.GenericIOBuffer{Array{UInt8,1}}) at iobuffer.jl:381
  ...

julia> w.databuf.data
ERROR: type Buffer has no field data

Since the databuf is not full nothing is written to the file of w.

julia> w.file
JLDFile /tmp/cdf21509-db64-4c3b-bb23-cdbfacd0b019.jld2 (read/write)
  (no datasets)

Let us continue triggering w until the databuf of w is full.

julia> for t in 2. : 5.
           put!(w.trigger, t)
           put!(w.input, [t * 10])
           take!(w.handshake)
       end
ERROR: MethodError: no method matching put!(::Inpin{Float64}, ::Float64)
Closest candidates are:
  put!(!Matched::Channel{T}, ::Any) where T at channels.jl:312
  put!(!Matched::Distributed.Future, ::Any) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.4/Distributed/src/remotecall.jl:557
  put!(!Matched::Distributed.RemoteValue, ::Any...) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.4/Distributed/src/remotecall.jl:572
  ...

Now check that the content of the file of w.

julia> w.file
JLDFile /tmp/cdf21509-db64-4c3b-bb23-cdbfacd0b019.jld2 (read/write)
  (no datasets)

Note that the content of databuf is written to the file of w. The operation of w can be terminated.

julia> terminate(w)
ERROR: MethodError: no method matching close(::Inpin{Float64})
Closest candidates are:
  close(!Matched::Base.DevNull) at coreio.jl:18
  close(!Matched::TextDisplay) at multimedia.jl:252
  close(!Matched::Base.Libc.FILE) at libc.jl:93
  ...

When terminated, the file of w is closed.

julia> w.file
JLDFile /tmp/cdf21509-db64-4c3b-bb23-cdbfacd0b019.jld2 (read/write)
  (no datasets)
Note

In this example, w does not have a plugin so nothing has been derived or computed from the data in databuf. The data in databuf is just written to file of w. To further data processing, see Plugins

Full API

Jusdl.WriterType
Writer(input=Inport(); buflen=64, plugin=nothing, callbacks=nothing, name=Symbol(uuid4()), 
    path=joinpath(tempdir(), string(name)))

Constructs a Writer whose input bus is input. buflen is the length of the internal buffer of Writer. If not nothing, plugin is used to processes the incomming data. path determines the path of the file of Writer.

Note

The type of file of Writer is JLD2.

Warning

When initialized, the file of Writer is closed. See open(writer::Writer) and close(writer::Writer).

source
Jusdl.freadMethod
fread(path::String)

Reads the content of jld2 file and returns the sorted file content.

source
Jusdl.write!Method
write!(writer, td, xd)

Writes xd corresponding to xd to the file of writer.

Example

julia> w = Writer(Inport(1))
Writer(path:/tmp/e907d6ad-8db2-4c4a-9959-5b8d33d32156.jld2, nin:1)

julia> open(w)
Writer(path:/tmp/e907d6ad-8db2-4c4a-9959-5b8d33d32156.jld2, nin:1)

julia> write!(w, 0., 10.)
10.0

julia> write!(w, 1., 20.)
20.0

julia> w.file
JLDFile /tmp/e907d6ad-8db2-4c4a-9959-5b8d33d32156.jld2 (read/write)
 ├─🔢 0.0
 └─🔢 1.0

julia> w.file[string(0.)]
10.0
source
Base.Filesystem.cpMethod
cp(writer::Writer, dst; force=false, follow_symlinks=false)

Copies the file of writer to dst. If force is true, the if dst is not a valid path, it is forced to be constructed. If follow_symlinks is true, symbolinks are followed.

Example

julia> mkdir(joinpath(tempdir(), "testdir1"))
"/tmp/testdir1"

julia> mkdir(joinpath(tempdir(), "testdir2"))
"/tmp/testdir2"

julia> w = Writer(Inport(), path="/tmp/testdir1")
Writer(path:/tmp/testdir1.jld2, nin:1)

julia> cp(w, "/tmp/testdir2")
Writer(path:/tmp/testdir2/1e72bad1-9800-4ca0-bccd-702afe75e555, nin:1)
source
Base.Filesystem.mvMethod
mv(writer::Writer, dst; force::Bool=false)

Moves the file of writer to dst. If force is true, the if dst is not a valid path, it is forced to be constructed.

Example

julia> mkdir(joinpath(tempdir(), "testdir1"))
"/tmp/testdir1"

julia> mkdir(joinpath(tempdir(), "testdir2"))
"/tmp/testdir2"

julia> w = Writer(Inport(), path="/tmp/testdir1/myfile.jld2")
Writer(path:/tmp/testdir1/myfile.jld2, nin:1)

julia> mv(w, "/tmp/testdir2")
Writer(path:/tmp/testdir2/myfile.jld2, nin:1)
source
Base.openMethod
open(writer::Writer)

Opens writer by opening the its file in read/write mode. When writer is not openned, it is not possible to write data in writer. See also close(writer::Writer)

source
Base.readMethod
read(writer::Writer, flatten=false)

Read the contents of the file of writer and returns the sorted content of the file. If flatten is true, the content is also flattened.

source