view julia/parallel/ParallelProcessing/src/json.jl @ 31:e3af2d3890ce

parallel processing in julia
author Lewin Bormann <lbo@spheniscida.de>
date Sun, 19 Mar 2023 13:09:54 +0100
parents
children 2d033830c26e
line wrap: on
line source

using JSON
using Random
import Base.Threads

struct Details
    level::String
    kind::Int
end

struct SimpleEntry
    s::String
    a::Int
    f::Float64
    details::Details
end

function generate_entry()::SimpleEntry
    s, level = randstring(20), randstring(10)
    a, kind = rand(Int, 2)
    f = rand()
    SimpleEntry(s, a, f, Details(level, kind))
end

function generate_json(file, n=1000)
    open(file; write=true) do fh
        for i = 1:n
            println(fh, json(generate_entry()))
        end
    end
end

fib(n) = if n <= 2 1 else fib(n-1) + fib(n-2) end

function expensive_mapper(m::Dict)::Int
    i = abs(m["a"]) % 35
    fib(i)
end

function process_json(file, mapper=x -> ())::Vector
    open(file; read=true) do fh
        [mapper(JSON.parse(line)) for line = eachline(fh)]
    end
end


function process_json_parallel(file, mapper=x -> ())::Vector
    open(file; read=true) do fh
        ch = Channel(100)
        count = 0
        for line = eachline(fh)
            Threads.@spawn put!(ch, mapper(JSON.parse(line)))
            count += 1
        end
        [take!(ch) for i = 1:count]
    end
end