All Projects → anykeyh → await_async

anykeyh / await_async

Licence: MIT license
Provide await and async methods to Crystal Lang

Programming Languages

crystal
512 projects

Projects that are alternatives of or similar to await async

workerpoolxt
Concurrency limiting goroutine pool without upper limit on queue length. Extends github.com/gammazero/workerpool
Stars: ✭ 15 (-78.87%)
Mutual labels:  concurrency
golang-101
🍺 In-depth internals, my personal notes, example codes and projects. Includes - Thousands of codes, OOP, Concurrency, Parallelism, Goroutines, Mutexes & Wait Groups, Testing in Go, Go tool chain, Backend web development, Some projects including Log file parser using bufio.Scanner, Spam Masker, Retro led clock, Console animations, Dictionary pro…
Stars: ✭ 61 (-14.08%)
Mutual labels:  concurrency
pygolang
Go-like features for Python and Cython. (mirror of https://lab.nexedi.com/kirr/pygolang)
Stars: ✭ 37 (-47.89%)
Mutual labels:  concurrency
kotlin-concurrency-primitives
Demo project that showcases the usage of various concurrency primitives in Java and Kotlin.
Stars: ✭ 32 (-54.93%)
Mutual labels:  concurrency
go-worker-thread-pool
A visual working example of a Thread Pool pattern, based on a known blog article.
Stars: ✭ 24 (-66.2%)
Mutual labels:  concurrency
wise-river
Object streaming the way it should be.
Stars: ✭ 33 (-53.52%)
Mutual labels:  concurrency
await-lock
Mutex locks for async functions
Stars: ✭ 66 (-7.04%)
Mutual labels:  concurrency
atomix
Simple and easy wrappers for Go sync/atomic package.
Stars: ✭ 26 (-63.38%)
Mutual labels:  concurrency
easy-promise-queue
An easy JavaScript Promise queue which is automatically executed, concurrency controlled and suspendable.
Stars: ✭ 31 (-56.34%)
Mutual labels:  concurrency
benchmark-http
No description or website provided.
Stars: ✭ 15 (-78.87%)
Mutual labels:  concurrency
orion
A Crystal router
Stars: ✭ 115 (+61.97%)
Mutual labels:  crystal-language
detox
distributed tox (tox plugin to run testenvs in parallel)
Stars: ✭ 48 (-32.39%)
Mutual labels:  concurrency
Leafgem
🌿💎 The humble beginnings of a 2D game engine in Crystal! [in-progress]
Stars: ✭ 72 (+1.41%)
Mutual labels:  crystal-language
swift-futures
Demand-driven asynchronous programming in Swift
Stars: ✭ 32 (-54.93%)
Mutual labels:  concurrency
TAOMP
《多处理器编程的艺术》一书中的示例代码实现,带有注释与单元测试
Stars: ✭ 39 (-45.07%)
Mutual labels:  concurrency
savetheworldwithgo
Build systems with Go examples
Stars: ✭ 81 (+14.08%)
Mutual labels:  concurrency
theater
Actor framework for Dart. This package makes it easier to work with isolates, create clusters of isolates.
Stars: ✭ 29 (-59.15%)
Mutual labels:  concurrency
traffic
Massively real-time traffic streaming application
Stars: ✭ 25 (-64.79%)
Mutual labels:  concurrency
treap
A thread-safe, persistent Treap (tree + heap) for ordered key-value mapping and priority sorting.
Stars: ✭ 23 (-67.61%)
Mutual labels:  concurrency
mongo orm
Mongo ORM: A simple ORM for using MongoDB with the crystal programming language, designed for use with Amber. Based loosely on Granite ORM. Supports Rails-esque models, associations and embedded documents.
Stars: ✭ 32 (-54.93%)
Mutual labels:  crystal-language

Await / Async

Build Status

Add await and async keywords to Crystal.

Installation

In your shards.yml:

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

Then:

require "await_async"

future = async fetch_something

do_some_computation_now

await future

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)

future = async check_website

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

async! / async

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

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

# vs

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

Usually, use async 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 |url|
    async! do
      HTTP::Client.get "https://#{url}"
    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

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].