Skip to content

Async versions for Reader and Writer #136

Open

Description

@splix

I'm wondering if there are any plans (or opposition to those) for async implementation for Reader and Writer? I.e., a reader from tokio::io::AsyncRead, etc

Activity

  1. splix commented on Feb 25, 2025

    @splix
    ContributorAuthor

    I've tried to add it, but quickly realized that it requires [mostly] duplicate versions for too many of internal functions.

    Like for

    pub fn zag_i64<R: Read>(reader: &mut R) -> AvroResult<i64> {
        let z = decode_variable(reader)?;
        Ok(if z & 0x1 == 0 {
            (z >> 1) as i64
        } else {
            !(z >> 1) as i64
        })
    }
    

    it needs an accompanying:

    #[cfg(feature = "tokio")]
    pub async fn zag_i64_async<R: AsyncRead>(reader: &mut R) -> AvroResult<i64> {
        let z = decode_variable_async(reader).await?;
        Ok(if z & 0x1 == 0 {
            (z >> 1) as i64
        } else {
            !(z >> 1) as i64
        })
    }
    

    and so on.

    It's definitely possible to implement, but the main question if that aligns well with avro-rs plans?

  2. martin-g commented on Feb 25, 2025

    @martin-g
    Member

    Duplicating the code is no go.

    IMO a more feasible way is to make everything async and use a same-thread thread pool for sync usage. But I haven't seen any other library doing it so probably I miss something.

  3. splix commented on Feb 25, 2025

    @splix
    ContributorAuthor

    For this particular example it's possible to move out the duplicate part into an inline function, and keep only decode_variable_async(reader).await?; and decode_variable_async(reader)?; depending on the variant. Another option is wrapping those with some macros which depending on the compilation target adds .await or not. Anyway, I agree that both options are bit ugly.

    Or actually I think that many functions like this one could be transformed to something more universal which could work in both ways. I'm worried mostly about the fact that too many things must be touched to support async.

  4. splix commented on Feb 25, 2025

    @splix
    ContributorAuthor
  5. martin-g commented on Apr 14, 2025

    @martin-g
    Member

    Closing the issue since there is no activity for almost 2 months now.
    We could re-open it if there are ideas how to do it!

  6. Kriskras99 commented on Jul 6, 2025

    @Kriskras99
    Contributor

    Reqwest is async by default, but has a blocking feature flag. It wraps the async types and spawns a Tokio thread to execute the async functions (https://github.com/seanmonstar/reqwest/blob/master/src/blocking/client.rs#L1234).

    Although reqwest uses Tokio, it might make sense to use smol to keep the impact on sync users small.

  7. martin-g commented on Jul 6, 2025

    @martin-g
    Member

    What would be the benefit of using async in Avro?
    File IO is not async anyway.
    Computation should be sync too.
    What would we gain ?

  8. Kriskras99 commented on Jul 6, 2025

    @Kriskras99
    Contributor

    I'm reading/writing files over the network, which is async. So currently I'm retrieving the entire file and then decoding it, instead of decoding it while it's downloading.

  9. splix commented on Jul 6, 2025

    @splix
    ContributorAuthor

    I have Avro on S3 too. So the whole code is async except the Avro writing/reading

  10. bmitc commented on Jul 17, 2025

    @bmitc

    This is a fairly large hiccup in work I am doing. Everything in the application, which makes file reads and HTTP requests, runs within Tokio, but to read data with the Avro reader, I need to use SyncIoBridge to be able to pass tokio_stream::Stream (via a StreamReader) data.

    What would be the benefit of using async in Avro?

    The benefits of async are not specific to Avro but apply to Avro. By this library being synchronous only, to read multiple files all concurrently, one needs to use threads rather than an asynchronous, task-based runtime like Tokio.

    File IO is not async anyway.
    Computation should be sync too.

    This is not accurate. Tokio provides asynchronous file utilities via tokio::fs. For example, tokio::fs::File is AsyncRead.

  11. Kriskras99 commented on Jul 17, 2025

    @Kriskras99
    Contributor

    File IO is not async anyway.
    Computation should be sync too.

    This is not accurate. Tokio provides asynchronous file utilities via tokio::fs. For example, tokio::fs::File is AsyncRead.

    It is accurate, under the hood Tokio spawns a "blocking" thread which reads the file using std::io and then pushes it to the async runtime

  12. Kriskras99 commented on Jul 17, 2025

    @Kriskras99
    Contributor

    @martin-g if you're interested I can write a mini-RFC discussing the options for an async variant/implementation, and after we've agreed (or decided it's not worth it) I can write the implementation.

  13. bmitc commented on Jul 17, 2025

    @bmitc

    File IO is not async anyway.
    Computation should be sync too.

    This is not accurate. Tokio provides asynchronous file utilities via tokio::fs. For example, tokio::fs::File is AsyncRead.

    It is accurate, under the hood Tokio spawns a "blocking" thread which reads the file using std::io and then pushes it to the async runtime

    It isn't accurate at a practical level. tokio::fs exists because there is a need to make file operations behave asynchronously. Whether or not the file operations are actually ascynrhonous is an implementation detail that doesn't support why there shouldn't be an asynchronous version of this Avro reader.

    The benefits of having this Avro library support AsyncWrite and AsyncRead are not tied to file operations but are tied to the reasons why async exists in the first place and allowing this Avro library to be more easily integrated into async and concurrent applications. If one wants to stream a lot of Avro data from many sources at the same time, then Tokio is the way to do that, not having to manually manage threads. SyncIoBridge works with this library, but it's awkward.

    Lastly, things that implement Read and AsyncRead do not always come from files. As many pointed out here, it's common to have Avro data in S3 or other such cloud-based systems, where the data is streamed over HTTP, which is asynchronous.

  14. martin-g commented on Jul 17, 2025

    @martin-g
    Member

    @martin-g if you're interested I can write a mini-RFC discussing the options for an async variant/implementation, and after we've agreed (or decided it's not worth it) I can write the implementation.

    I am very much interested !
    Any help is very welcome!

  15. Kriskras99 commented on Jul 21, 2025

    @Kriskras99
    Contributor

    @bmitc some questions to help with my RFC:

    • Do you use the Serde implementation or are you working with "raw" avro::Values?
    • What async runtime are you using?
  16. martin-g commented on Jul 21, 2025

    @martin-g
    Member
    • What async runtime are you using?

    #136 (comment) says that (s)he uses Tokio

  17. martin-g commented on Sep 18, 2025

    @martin-g
    Member

    Hi,

    At #246 you may find an implementation of async/sync APIs generation with SyncA library.
    We will be grateful if you can give it a try and report issues and ideas for improvement!

    You can read how it works at https://martin-grigorov.medium.com/generate-a-sync-apis-of-a-rust-library-edf3b72b1ea3

  18. martin-g commented on Sep 30, 2025

    @martin-g
    Member

    It seems there is no more interest in this functionality.
    It is rather hard to keep the async-with-synca branch in sync with main due to the refactorings.
    I am considering to abandon async-with-synca due to the lack of interest and the complexities it brings.

  19. Kriskras99 commented on Sep 30, 2025

    @Kriskras99
    Contributor

    I'm definitely still interested in this, but have been rather busy in my personal life and at work.

  20. bmitc commented on Sep 30, 2025

    @bmitc

    I am definitely interested in it as well. The project that I am using this Avro library for is in the throes of a deadline, and I am just not able to experiment with the async implementation right now. Perhaps in a few weeks I could try it out.

  21. self-assigned this
    on Dec 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions