{"id":27428635,"url":"https://github.com/fvd/futureplumber","last_synced_at":"2025-04-14T13:39:06.996Z","repository":{"id":71640003,"uuid":"70523280","full_name":"FvD/futureplumber","owner":"FvD","description":"Using future with plumber to set up asynchronous api calls ","archived":false,"fork":false,"pushed_at":"2016-10-10T20:11:36.000Z","size":2,"stargazers_count":14,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-11-26T07:04:04.538Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/FvD.png","metadata":{"files":{"readme":"README.Rmd","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-10-10T19:53:05.000Z","updated_at":"2024-11-07T15:23:13.000Z","dependencies_parsed_at":"2023-02-23T08:16:04.497Z","dependency_job_id":null,"html_url":"https://github.com/FvD/futureplumber","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FvD%2Ffutureplumber","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FvD%2Ffutureplumber/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FvD%2Ffutureplumber/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FvD%2Ffutureplumber/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/FvD","download_url":"https://codeload.github.com/FvD/futureplumber/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248889647,"owners_count":21178267,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-04-14T13:39:06.221Z","updated_at":"2025-04-14T13:39:06.980Z","avatar_url":"https://github.com/FvD.png","language":"R","funding_links":[],"categories":[],"sub_categories":[],"readme":"---\ntitle: \"Future Plumber\"\noutput: github_document\n---\n\n```{r setup, include=FALSE}\nknitr::opts_chunk$set(echo = TRUE)\n```\n\n## Asynchronous API's with Plumber\nPlumber does not have in-built features to handle calls to the endpoints asynchronously. And perhaps this is something that need not be handled by plumber directly, but instead can be handled by the [future](https://github.com/HenrikBengtsson/future) package. A huge benefit is that **future** can do much more than just handling code asynchronously.\n\nSo as a very basic and first example we implement this example from the future README in plumber\n\n```{r}\nlibrary(future)\nplan(multisession)\npid \u003c- Sys.getpid()\npid\n```\n### Multiple pids\n```{r}\na %\u003c-% {\n    cat(\"Resolving 'a' ...\\n\")\n    Sys.getpid()\n}\nb %\u003c-% {\n    cat(\"Resolving 'b' ...\\n\")\n    Sys.getpid()\n}\nc %\u003c-% {\n    cat(\"Resolving 'c' ...\\n\")\n    Sys.getpid()\n}\na\nb\nc\n```\n\nIt is not possible to use the `%\u003c-%` directly in plumber, it has to be placed inside a function, like so:\n\n```{r}\nin_a_function \u003c- function(){\n  a %\u003c-% {\n       cat(\"Resolving 'a' ...\")\n       Sys.sleep(2)\n       cat(\"done\\n\")\n       current_pid \u003c- Sys.getpid()\n       return(current_pid)\n  }\n  cat(\"Solved in pid: \", a)\n}\n```\n\nAnd this is the basis for the first example in the folder [multiprocess], that uses `plan(multiprocess)` to handle all calls asynchronously (instantiates a new R process for each call).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffvd%2Ffutureplumber","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffvd%2Ffutureplumber","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffvd%2Ffutureplumber/lists"}