R控制台可以支持后台任务还是中断(事件处理)? [英] Can the R console support background tasks or interrupts (event-handling)?

查看:202
本文介绍了R控制台可以支持后台任务还是中断(事件处理)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在R控制台中工作时,我想设置一个后台任务来监视特定的连接,当事件发生时,执行另一个功能(警报)。或者,我可以设置一些事情,以便外部函数只是向R发送警报,但这似乎是同样的问题:需要设置一个监听器。

While working in an R console, I'd like to set up a background task that monitors a particular connection and when an event occurs, another function (an alert) is executed. Alternatively, I can set things up so that an external function simply sends an alert to R, but this seems to be the same problem: it is necessary to set up a listener.

我可以在R的专用过程中执行此操作,但是我不知道在控制台中是否可行。另外,如果控制台正在等待输入,我不会中断R,如果控制台只是在等待输入,则提醒或中断。

I can do this in a dedicated process of R, but I don't know if this is feasible from within a console. Also, I'm not interested in interrupting R if it is calculating a function, but alerting or interrupting if the console is merely waiting on input.

这里有三个用例:


  1. 最简单的例子就是观看文件。假设我有一个名为latestData.csv的文件,我想监视它的更改;当它更改时,执行 myAlert()。 (可以扩展它做不同的事情,但只是弹出一个文件已更改的注释是有用的。)

  1. The simplest possible example is watching a file. Suppose that I have a file called "latestData.csv" and I want to monitor it for changes; when it changes, myAlert() is executed. (One can extend it to do different things, but just popping up with a note that a file has changed is useful.)

另一种监视器注意给定的机器是否在RAM上运行的低,并且可能执行 save.image()并终止。再次,这可能是一个简单的问题,观看由外部监视器生成的文件,该文件保存 top 或其他命令的输出。

A different kind of monitor would watch for whether a given machine is running low on RAM and might execute a save.image() and terminate. Again, this could be a simple issue of watching a file produced by an external monitor that saves the output of top or some other command.

另一个例子就像另一个最近的SO问题,关于:让R停止运行的EC2机器。如果来自另一台机器或进程的警报告诉程序保存&终止,然后能够听到这个警报会很棒。

A different example is like another recent SO question, about : have R halt the EC2 machine it's running on. If an alert from another machine or process tells the program to save & terminate, then being able to listen for that alert would be great.

目前,我怀疑有两个处理方式:通过Rserve,可能通过 fork 。如果有人有任何一个例子,如何做到这一点与任何包或通过另一种方法,这将是巨大的。我认为解决这三个用例中的任何一个都可以解决所有这些问题,模数一点外部代码。

At the moment, I suspect there are two ways of handling this: via Rserve and possibly via fork. If anyone has examples of how to do this with either package or via another method, that would be great. I think that solving any of these three use cases would solve all of them, modulo a little bit external code.

注意1:我意识到,根据这个答案另一个SO问题 R是单线程,这就是为什么我怀疑 fork 和Rserve可能工作。但是,如果与R终端连接,我不确定可行性。虽然R的REPL附加到控制台的输入,但我试图绕过这个或模仿它,这是 fork Rserve 可能是答案。

Note 1: I realize, per this answer to another SO question that R is single threaded, which is why I suspect fork and Rserve may work. However, I'm not sure about feasibility if one is interfacing with an R terminal. Although R's REPL is attached to the input from the console, I am trying to either get around this or mimic it, which is where fork or Rserve may be the answer.

注2:对于熟悉事件处理/事件处理方法的人员,也可以解决所有问题。我刚刚在R中没有发现任何关于这个问题的信息。

Note 2: For those familiar with event handling / eventing methods, that would solve everything, too. I've just not found anything about this in R.

更新1:我发现写作手册R扩展具有引用事件处理的部分,它提到使用 R_PolledEvents 。这看起来很有希望。

Update 1: I've found that the manual for writing R extensions has a section referencing event handling, which mentions the use of R_PolledEvents. This looks promising.

推荐答案

这取决于你是否要中断空转或工作R.如果第一个,你可以想到绕过一些事件侦听器的R默认REPL循环将排队传入事件并对其进行评估。常用的选项是使用tcl / tk或gtk循环;我在我的 triggr 软件包中已经在libev上完成了这样的事情,这使得R摘要请求来自一个套接字。

It depends whether you want to interrupt idling or working R. If the first, you can think of bypassing the R default REPL loop by some event listener that will queue the incoming events and evaluate them. The common option is to use tcl/tk or gtk loop; I have made something like this around libev in my triggr package, which makes R digest requests coming from a socket.

后一种情况大多是绝望的,除非您手动使计算代码执行 if(甚至Occured)processIt 代码定期。

The latter case is mostly hopeless, unless you will manually make the computational code to execute if(evenOccured) processIt code periodically.

多线程并不是一个真正的选择,因为你知道在一个进程中的两个口译员会通过使用相同的方式打破自己全局变量,而分叉进程将具有独立的内存内容。

Multithreading is not a real option, because as you know two interpreters in one process will break themselves by using same global variables, while forked processes will have independent memory contents.

这篇关于R控制台可以支持后台任务还是中断(事件处理)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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