# How to wrap a network I/O library? (i.e. how to teach libuv to yield)

**URL:** https://discourse.julialang.org/t/how-to-wrap-a-network-i-o-library-i-e-how-to-teach-libuv-to-yield/76920
**Category:** General Usage
**Tags:** question
**Created:** [February 22, 2022, 4:39pm UTC](https://discourse.julialang.org/t/how-to-wrap-a-network-i-o-library-i-e-how-to-teach-libuv-to-yield/76920 "2022-02-22T16:39:41Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [February 22, 2022, 4:39pm UTC](https://discourse.julialang.org/t/how-to-wrap-a-network-i-o-library-i-e-how-to-teach-libuv-to-yield/76920/1 "2022-02-22T16:39:41Z")

</div>

from:

> [@@async calls to C API yields?](https://discourse.julialang.org/t/async-calls-to-c-api-yields/15399/3):
>
> But then I don’t think you can do the IO while calling yield? I suppose all the IO has to go through the event loop in order to do them concurrently (non-blockingly). IIUC, if you need to do IO without touching libuv’s IO interface, then you have to do it in a different thread. To ping Julia to come back to the C function in the main thread, you call uv\_async\_send(cond.handle) from the IO thread once the IO is finished, where this cond is created with [Base.AsyncCondition](https://docs.julialang.org/en/v1/base/base/#Base.AsyncCondition). You can call Julia …

I can vaguely understand the scheme, but concretely, what should I do, assuming I have these functions wrapped:

```julia
function _read!(ptr, fobj, nb, seekloc)
    @ccall lib.ReadAt(ptr::Ptr{UInt8}, 
                      fobj.gofile_id::Cstring, nb::Clong, seekloc::Clong)::Cvoid
end

function Base.read(fobj::XRDStream, nb::Integer)
    buffer = Vector{UInt8}(undef, nb)
    GC.@preserve buffer _read!(buffer, fobj, nb, fobj.seekloc)
    fobj.seekloc += nb
    return buffer
end

```

how to modify the `Base.read()` such that it yields to other tasks when waiting for the `ccall` to finish?
