# How to write an asynchronous version of the following code?

**URL:** <https://discourse.julialang.org/t/how-to-write-an-asynchronous-version-of-the-following-code/104657>\
**Category:** General Usage\
**Tags:** question, async\
**Created:** [October 6, 2023, 10:47am UTC](https://discourse.julialang.org/t/how-to-write-an-asynchronous-version-of-the-following-code/104657 "2023-10-06T10:47:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xiaoguang\_Pan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaoguang_pan/32/202282_2.png) [@Xiaoguang\_Pan](https://discourse.julialang.org/u/Xiaoguang_Pan)\
**Post date:** [October 6, 2023, 10:47am UTC](https://discourse.julialang.org/t/how-to-write-an-asynchronous-version-of-the-following-code/104657/1 "2023-10-06T10:47:01Z")

</div>

```julia
function parseFiles(inputFile::String,outFile::String)
    outf = open(outFile,"w")
    ### read in and process 
    for line in eachline(inputFile)
        if startswith(line,"#")
            continue
        else
            fields = split(line,'\t')
            ## write line by line
            println(outf,fields[1],"-",fields[2],"-",fields[3],"-",fields[4],"\t",fields[5],"\t",fields[6])
        end
    end
    close(outf)
end

```

This code will read a txt file line by line, separate each line by “\t,” and combine some columns, thus writing selected columns into a new file. How do we write this code as an asynchronous version to balance time usage between IO and computing ?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 6, 2023, 12:22pm UTC](https://discourse.julialang.org/t/how-to-write-an-asynchronous-version-of-the-following-code/104657/2 "2023-10-06T12:22:44Z")

</div>

Welcome!

What computing? There is basically only IO in your example, and probably that will be just bottle-necked by the reading of the data from the disc.

---

<div class="post-metadata">

**Author:** ![Xiaoguang\_Pan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaoguang_pan/32/202282_2.png) [@Xiaoguang\_Pan](https://discourse.julialang.org/u/Xiaoguang_Pan)\
**Post date:** [October 6, 2023, 1:27pm UTC](https://discourse.julialang.org/t/how-to-write-an-asynchronous-version-of-the-following-code/104657/3 "2023-10-06T13:27:17Z")

</div>

Yes, exactly. But generally, reading is faster than writing. Will the `println` influence the reading and splitting operations?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 6, 2023, 1:41pm UTC](https://discourse.julialang.org/t/how-to-write-an-asynchronous-version-of-the-following-code/104657/4 "2023-10-06T13:41:57Z")

</div>

I’m not an expert on optimization of IO operations, but my impression is that with such a minimal example, where it is not clear _why_ you need to optimize the code, it will be hard to get help.

Maybe you will get better feedback if you provide a more clear picture of what exactly are you trying to achieve in a larger scale.

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [October 6, 2023, 4:54pm UTC](https://discourse.julialang.org/t/how-to-write-an-asynchronous-version-of-the-following-code/104657/5 "2023-10-06T16:54:38Z")

</div>

`Channel`s are nice for coordinating asynchronous code:

```julia
function parseFilesAsync(inputFile::String,outFile::String)
    buf_size = 10
    chan = Channel{Vector{String}}(buf_size; spawn = true) do ch
        ### read in and process 
        for line in eachline(inputFile)
            if startswith(line,"#")
                continue
            else
                fields = split(line,'\t')
                put!(ch, fields)
            end
        end
    end
    
    open(outFile, "w") do outf
        for fields in chan
            println(outf, join(fields, "-"))
        end
    end
end

```
