Giter VIP home page Giter VIP logo

await_async's Introduction

Await / Async

Add await and async keyword in Crystal.

Installation

In your shards.yml:

dependencies:
  await_async:
    github: anykeyh/await_async
    branch: master

Then:

require "await_async"

x = async fetch_something

do_some_computation_now

await x

Usage

  • Call async on any method or block to create a MiniFuture
  • Call await on any MiniFuture to wait for/get the result
  • Conveniently, you can call await on future's array.

Can improve drastically application which relay on blocking IO like web API or file writing.

await(timeout, future)

f = async check_website

begin
  await 5.seconds, f
rescue MiniFuture::TimeoutException
  # rescue from timeout
end

async_lp

By default, async call the newly created fiber just after creation.

  • You can use instead async_lp so the fiber won't start now:
f = async{ 1 + 2 }
# At this moment the result is already computed
# f.finished? == true
await f # return 3

#vs

f = async_lp{ 1 + 2 }
# Here the result is not computed
# f.finished? == false
await f #Compute now

Usually, use async_lp if your block is computation intensive and current thread has IO blocking operation. Use async in other cases.

In case of errors, the exception will be raise at await moment, in the await thread.

MiniFuture

A minimalist version of future. Has finished? and running? methods.

I don't use Crystal's Concurrent::Future class because :nodoc:

Why?

Because crystal is great for building CLI tools. And CLI deals a lot with files and sockets. And IO performed in main thread are slow.

Usage of Channel is recommended for complex software, as it offers more patterns.

await/async is useful to build fast and deliver fast.

I don't want await/async to be exported in the global scope

  1. require await_async/helper instead of await_async
  2. In the class/module you want to use the methods, add include AwaitAsync::Helper. You can also simply call await/async directly from AwaitAsync::Helper

Example

def fetch_websites_async
  %w(www.github.com
  www.yahoo.com
  www.facebook.com
  www.twitter.com
  crystal-lang.org).map do |ws|
    async do
      HTTP::Client.get "https://#{ws}"
    end
  end
end

# Process the websites concurrently. Start querying another website when the
# first one is waiting for response
await(5.seconds, fetch_websites_async).each do |response|
 #...
end

Licence

MIT

await_async's People

Contributors

anykeyh avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.