Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ixpantia/asyncio
An Async Reader in R
https://github.com/ixpantia/asyncio
Last synced: 8 days ago
JSON representation
An Async Reader in R
- Host: GitHub
- URL: https://github.com/ixpantia/asyncio
- Owner: ixpantia
- License: other
- Created: 2024-02-22T01:40:36.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-03-05T19:14:40.000Z (9 months ago)
- Last Synced: 2024-08-13T07:15:44.770Z (4 months ago)
- Language: Rust
- Size: 19.5 KB
- Stars: 7
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- jimsghstars - ixpantia/asyncio - An Async Reader in R (Rust)
README
# asyncio
As the name suggests, this a very simple async reader and writer for
interacting with files in a non-blocking way from R. It runs the file I/O
in a separate thread and uses the
[promises](https://rstudio.github.io/promises/index.html) as an API to
eventually return the result of the operation.## Installation
> **Note**: This package is not yet on CRAN and is still just an experiment.
> You will also need `rustc` and `cargo` installed in your system to compile the
> Rust code.```r
# With remotes
remotes::install_github("ixpantia/asyncio")
# With pak
pak::pak("ixpantia/asyncio")
```## Usage
This package works best when used inside a plumber API or any other async environment like
Shiny. Here is an example of how to use it in a Plumber API to asynchronously read and write
to a file.```R
library(asyncio)
library(promises)options(asyncio.threads = 10)
temp_file <- tempfile()#* @post /write
#* @param line The line to write to the file
#* @parser text
function(line) {
asyncWriteLines(line, temp_file, append = TRUE) %...>% {
paste("You wrote:", ., "bytes to the file")
} %...!% {
paste("Failed to write to the file:", .)
}
}#* @get /read
function() {
asyncReadLines(temp_file) %...!% {
paste("Failed to read from the file, try writing something first:", .)
}
}
```## Changing the number of worker threads
`asyncio` uses actual operating system threads, not additional R proccesses,
therefore spawning additional threads is not as expensive as it would be if
using the `future` package. You can change the number of worker threads by
setting the option `asyncio.threads` before calling any of the async functions.By default, `asyncio.threads` is set to 4. You can change it like this:
```R
options(asyncio.threads = 10)
```## Improvements
1. Add more file reading functions.
1. Add more options for the file reading functions.
1. Add other I/O operations like TCP, UDP, etc.