An open API service providing package, version and dependency metadata of many open source software ecosystems and registries.

Top 6.1% on proxy.golang.org

proxy.golang.org : github.com/b97tsk/async

Package async is a library for asynchronous programming. Since Go has already done a great job in bringing green/virtual threads into life, this library only implements a single-threaded Executor type, which some refer to as an async runtime. One can create as many executors as they like. While Go excels at forking, async, on the other hand, excels at joining. Wanted to execute pieces of code from goroutines in a single-threaded way? An Executor is designed to be able to run tasks spawned in goroutines sequentially. This comes in handy when one wants to do a series of operations on a single thread, for example, to read or update states that are not safe for concurrent access, to write data to the console or a file, to update one's user interfaces, etc. Be aware that there is no back pressure. Task spawning isn't designed to block. If spawning outruns execution, an Executor can easily consume a lot of memory over time. To mitigate, one could introduce a semaphore per hot spot. An async Task can be reactive. An async Task is spawned with a Coroutine to take care of it. In this user-provided function, one can return a specific Result to tell a coroutine to watch and await some events (e.g. Signal, State and Memo, etc.), and the coroutine can just re-run the task whenever any of these events notifies. This is useful when one wants to do something repeatedly. It works like a loop. To exit this loop, just return a Result that does something different from within the task function. Simple. A Coroutine can also make a transition from one Task to another, just like a state machine can make a transition from one state to another. This is done by returning another specific Result from within a task function. A coroutine can transition from one task to another until a task ends it. With the ability to transition, async is able to provide more advanced control structures, like Block, Loop and Func, to ease the process of writing async code. The experience now feels similar to that of writing sync code. It's not recommended to have channel operations in an async Task for a Coroutine to do, since they tend to block. For an Executor, if one coroutine blocks, no other coroutines can run. So instead of passing data around, one would just handle data at places where data are available. Async tasks are quite flexible and composable. One can even build a state machine across goroutine boundaries, which isn't something channels are qualified to do. Async tasks are better building blocks than channels. One of the advantages of passing data over channels is to be able to avoid allocation. Unfortunately, async tasks always escape to heap. Any variable they capture also escapes to heap. One should stay alert and take measures in hot spots, like repeatedly using a same task. Async encourages non-blocking programming, which makes structured concurrency quite essential to this library. At some point, one might want to know when an Executor stops operating. In Go, all Go code can only be executed by goroutines. Async tasks are just ordinary Go functions, too. If one keeps track of every goroutine that spawns async tasks, and waits for them to finish, then the exact right time when an Executor stops operating can be determined too. Essentially, it creates a synchronization point between the spawned tasks and the rest of the code after the waiting. This example demonstrates how a task can conditionally depend on a state. This example demonstrates how a memo can conditionally depend on a state. This example demonstrates how to end a task. It creates a task that prints the value of a state whenever it changes. The task only prints 0, 1, 2 and 3 because it is ended after 3. This example demonstrates how to use memos to memoize cheap computations. Memos are evaluated lazily. They take effect only when they are acquired. This example demonstrates how to set up an autorun function to run an executor in a goroutine automatically whenever a coroutine is spawned or resumed. This example demonstrates how a coroutine can transition from one task to another.

Registry - Source - Documentation - JSON
purl: pkg:golang/github.com/b97tsk/async
Keywords: async , async-await , coroutines , event-driven , go , golang , single-threaded , state-machines
License: MIT
Latest release: about 1 month ago
First release: 9 months ago
Namespace: github.com/b97tsk
Stars: 0 on GitHub
Forks: 0 on GitHub
See more repository details: repos.ecosyste.ms
Last synced: 6 days ago

    Loading...
    Readme
    Loading...