R中的异步编程 [英] Asynchronous programming in R

查看:129
本文介绍了R中的异步编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序(用R语言),该程序在特定的指定时间进行API调用. API调用需要一些时间,但是在进行API调用时,我需要计时器(主循环)以继续计数.为此,我需要将API调用外包"到另一个CPU线程.我相信这是有可能的,并且已经研究了futurepromises软件包,但是还没有找到解决方案.

I am writing a program (in R) that makes API calls at certain designated times. The API calls take a while, but I need the timer (main loop) to continue counting while the API call is made. To do so, I need to "outsource" the API call to another CPU thread. I believe this is possible and have looked into the future and promises packages, but haven't found a solution yet.

让我们运行一个从0到100计数的for循环.当计数器(i)达到50时,它必须完成一个资源密集的过程(调用函数sampler,该过程对100万个样本进行采样正则分布10,000次以节省计算空间).希望计数器在sampler()在另一个线程上工作时继续计数.

Let's run a for loop that counts from 0 to 100. When the counter (i) gets to 50, it has to complete a resource-intensive process (calling the function sampler, which samples 1 million normal distributions 10,000 times for the sake of taking up computation space). The desire is for the counter to continue counting while sampler() is doing its work on another thread.

#Something to take up computation space
sampler <- function(){
  for(s in 1:10000) sample(1000000)
}

#Get this counter to continue while sampler() runs on another thread
for(i in 1:100){
  message(i)
  if(i == 50){
    sampler()
  }
}

我尝试过的(失败)

library(future)

sampler <- function(){
  for(s in 1:10000) sample(1000000)
}

for(i in 1:100){
  message(i)
  if(i == 50){
    mySamples <- future({ sampler() }) %plan% multiprocess
  }
}

推荐答案

在我看来,您的呼叫仅在创建工作人员时阻塞,而在实际工作期间却没有.例如.如果先执行plan(),则计数器不会阻塞:

It seems to me your call is only blocking while the workers are created, but not for the duration of the actual work. E.g. if do the plan() first, the counter will not block:

library(future)

sampler <- function(){
  for(s in 1:10000) sample(1000000)
}

plan(multiprocess)

for(i in 1:100){
  message(i)
  if(i == 50){
    mySamples <- future({ sampler() })
  }
}

还请注意,sampler()的运行时间比代码中阻塞调用的持续时间长得多,并且在执行代码后,mySamples仍处于状态resolved: FALSE,并且CPU使用率仍然很高

Also note, that the runtime of sampler() is much longer than the duration of the blocking call in your code and that, after executing your code, mySamples still has the status resolved: FALSE and CPU usage is still high.

这篇关于R中的异步编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆