Skip to content

bridging sync/async in "hobby code" where you don't care that much #54

@nikomatsakis

Description

@nikomatsakis
  • Brief summary:
    • Hacking on a simple client that has to make api calls but where performance isn't much of a priority; many of the libraries are written in an async style, but sometimes you want to make those calls in a location where you can't conveniently make the code async (e.g., an iterator).
    • Currently trying to manage this situation involves either propagating async everywhere or adding block_on calls. But, at least in tokio, block_on calls cannot execute from async threads, so this can lead to panics if that code that is using block_on ever winds up in an async context.
  • Character: Barbara, this was sourced from a conversation with @Mark-Simulacrum

More details from my conversation with @Mark-Simulacrum

  • Context: building things like perf.rust-lang.org, triagebot
  • Performance is not a real issue here, convenience and expressiveness is
    • only have so much time to work on this, trying to stand something up quickly
  • Want a way to make calls to various web services, connect to databases, or do other parts of I/O
  • Would be happy with sync, but the libraries and things are in async
  • Didn't really want to have to pick a runtime, doesn't care much, mostly wants things to Just Work. In the end picked tokio as the only widely known tool at the time, as well as the one that is compatible with things like defaults in hyper.
  • Sometimes find themselves in a synchronous context but need to do an async operation
    • e.g., implementing an iterator
  • Don't care too much about performance, so add a block_on
    • But block-on doesn't take an async block
    • So often nest a spawn inside
    • But then that code gets invoked in an async context, and the code panics
    • Frustrating -- how bad of a problem is it really?
    • Example code
  • Gets in the scenario (example) where
    • something internally has to be make an http request
    • do we make it async?
      • that forces all callers to be async
    • or sync with block-on
      • then cannot be used from an async context
    • or just sync?
      • but then need to find a different http lib (can't use reqwest/hyper)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions