# @async and SQLite.jl

**URL:** https://discourse.julialang.org/t/async-and-sqlite-jl/87326
**Category:** New to Julia
**Created:** [September 15, 2022, 6:21pm UTC](https://discourse.julialang.org/t/async-and-sqlite-jl/87326 "2022-09-15T18:21:49Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Rafael\_Brus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael_brus/32/23767_2.png) [@Rafael\_Brus](https://discourse.julialang.org/u/Rafael_Brus)
#### Post date: [September 15, 2022, 6:21pm UTC](https://discourse.julialang.org/t/async-and-sqlite-jl/87326/1 "2022-09-15T18:21:49Z")

</div>

Is there any way to use @async with SQLite.jl? The execution of a task only starts after the other one finishes in my tests.

```julia
function block_sql(n::Int64, tam::Int64)  
    local results = Channel(32);
    local steps = convert(Int64, round(tam/n)) + 1
   
    function do_work(i)
        db = SQLite.DB(joinpath("data", "linksus.db"))
        # Blocagem
        println("Processo $i")
        inicio = (i * steps) - steps
        fim = i * steps
        sql = """
            SELECT
                b1."index" as id1,
                b2."index" as id2,
                b1.nome as nome1,
                b2.nome as nome2,
                b1.nome_mae as nm_m1,
                b2.nome_mae as nm_m2,
                b1.dn as dn1,
                b2.dn as dn2
                    
            from b1_proc as b1            
                inner join b2_proc as b2 on b2.dn = b1.dn or 
                (b2.sxpn = b1.sxpn and b2.sxun = b1.sxun and b2.sxpnm = b1.sxpnm and not b2.sxpnm is null) or
                (b2.sxpn = b1.sxpn and b2.sxsn = b1.sxsn and b2.sexo = b1.sexo) or
                (b2.sxun = b1.sxun and strftime('%Y',b2.dn) = strftime('%Y',b1.dn))

            where
                b1."index" >= $inicio and b1."index" <= $fim and
                not b1."index" is null and not b2."index" is null
                
            order by id1, id2
        """
        
        df = DBInterface.execute(db, sql) |> DataFrame
        show(df)
                   
        put!(results, (df))
        
    end;

    for i in 1:n # start 4 tasks to process requests in parallel
        errormonitor(@async do_work(i))
        # @async begin
        # do_work(i)
        # end
    end

    local stps = 2
    @elapsed while stps > 0 # print out results
        df = take!(results)
        println(size(df, 1))
        stps = stps - 1
    end
        
end

```

---

<div class="post-metadata">

### Author: ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)
#### Post date: [September 15, 2022, 6:55pm UTC](https://discourse.julialang.org/t/async-and-sqlite-jl/87326/2 "2022-09-15T18:55:07Z")

</div>

Hmmmm, I would have thought this would just work. But then again, I seem to remember there was maybe a specific compile flag you had to pass in order for concurrency to be allowed in the sqlite database. Might have to try and research if that’s a thing.

---

<div class="post-metadata">

### Author: ![stephancb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephancb/32/14243_2.png) [@stephancb](https://discourse.julialang.org/u/stephancb)
#### Post date: [September 15, 2022, 9:02pm UTC](https://discourse.julialang.org/t/async-and-sqlite-jl/87326/3 "2022-09-15T21:02:07Z")

</div>

A relevant web page is [here](https://www.sqlite.org/threadsafe.html). The default mode is _serialized_, but perhaps _multi-threaded_ is needed in this case. _multi-threaded_ mode can be set with the [int sqlite3\_config(int, …);](https://www.sqlite.org/c3ref/config.html) C function, before the tasks are started.
