首页 \ 问答 \ 守护进程与Clojure / JVM(Daemon with Clojure/JVM)

守护进程与Clojure / JVM(Daemon with Clojure/JVM)

我想在一个小服务器上运行一个小的(不是太多太多)守护进程,看一个目录添加到它的新文件(以及主程序中的任何目录),并调用另一个Clojure程序来处理那个新文件。

理想情况下,每个文件都会被添加到一个队列(由Clojure中的ref表示的列表?),并且主进程将以FIFO为基础处理队列中的那些文件。

我的问题是:有一个JVM一直在运行这个小程序太多资源吗? 你有什么建议去做这个吗?

非常感谢你!

编辑:我应该问的另一个问题:我应该将它作为自己的实例运行(使用更少的内存)并让它在看到文件时启动新的JVM,或者在同一个JVM上运行将处理文件的Clojure代码?


I'd like to have a small (not doing too damn much) daemon running on a little server, watching a directory for new files being added to it (and any directories in the main one), and calling another Clojure program to deal with that new file.

Ideally, each file would be added to a queue (a list represented by a ref in Clojure?) and the main process would take care of those files in the queue on a FIFO basis.

My question is: is having a JVM up running this little program all the time too much a resource hog? And do you have any suggestions as to how go about doing this?

Thank you very much!

EDIT: Another question I should ask: should I run this as its own instance (using less memory) and have it launch a new JVM when a file is seen, or have it on the same JVM the Clojure code that will process the file?


原文:https://stackoverflow.com/questions/2933280
更新时间:2024-01-18 08:01

最满意答案

拦截部分有一个错误(paramsA)。 其他一切都很好。 我已经实施了Alexey在评论中提出的建议。 这是解决方案:

pow <- function(x,y)
{
  return(x^y)
}


#### posterior distribution
posteriorDistribution <- function(x, y, a, b,s2,N)
{
sumSqError <- 0.0
for(i in 1:N)
{
  sumSqError <- sumSqError + pow(y[i] - (a + b*x[i]),2)
}
return((-((N/2)+1) * log(s2)) + ((-0.5/s2) * sumSqError))

}

# x <- x values
# y <- actual datapoints
# N <- sample size
# m <- length of chain
# sigmaProposalWidth <- width of uniform proposal dist for sigma squared
# paramAProposalWidth <- width of uniform proposal dist for intercept
# paramBProposalWidth <- width of uniform proposal dist for slope

mcmcSampling <- function(x,y,N,m,sigmaProposalWidth,paramAProposalWidth,paramBProposalWidth)
{

  desiredAcc <- 0.44

  paramsA <- vector("numeric",length=m) # intercept
  paramsB <- vector("numeric",length=m) # slope
  s2 <- vector("numeric",length=m) # sigma squared

  paramsA[1] <- 0
  paramsB[1] <- 0
  s2[1] <- 1

  accATot <- 0
  accBTot <- 0
  accS2Tot <- 0

  for(i in 2:m)
  {
    paramsA[i] <- paramsA[i-1] + runif(1,-paramAProposalWidth,paramAProposalWidth)
    accA <- 1
    if((posteriorDistribution(x,y,paramsA[i],paramsB[i-1],s2[i-1],N) - 
        posteriorDistribution(x,y,paramsA[i-1],paramsB[i-1],s2[i-1],N)) < log(runif(1)))
    {
      paramsA[i] <- paramsA[i-1]
      accA <- 0
    }


    accATot <- accATot + accA

    paramsB[i] <- paramsB[i-1] + runif(1,-paramBProposalWidth,paramBProposalWidth)
    accB <- 1
    if((posteriorDistribution(x,y,paramsA[i],paramsB[i],s2[i-1],N) - 
        posteriorDistribution(x,y,paramsA[i-1],paramsB[i-1],s2[i-1],N)) < log(runif(1)))
    {
      paramsB[i] <- paramsB[i-1]
      accB <- 0
    }

    accBTot <- accBTot + accB

    s2[i] <- s2[i-1] + runif(1,-sigmaProposalWidth,sigmaProposalWidth)
    accS2 <- 1

    if((s2[i] < 0) || (posteriorDistribution(x,y,paramsA[i],paramsB[i],s2[i],N) - 
                       posteriorDistribution(x,y,paramsA[i],paramsB[i],s2[i-1],N)) < log(runif(1)))
    {
      s2[i] <- s2[i-1]
      accS2 <- 0
    }

    accS2Tot <- accS2Tot + accS2

    if(i%%100==0)
    {

      paramAProposalWidth <- paramAProposalWidth * ((accATot/100)/desiredAcc)
      paramBProposalWidth <- paramBProposalWidth * ((accBTot/100)/desiredAcc)
      sigmaProposalWidth <- sigmaProposalWidth * ((accS2Tot/100)/desiredAcc)

      accATot <-  0
      accBTot <-  0 
      accS2Tot <-  0

    }


  }
    res <- data.frame(paramsA,paramsB,s2)
    return(res)

}

There was one mistake in the intercept section (paramsA). Everything else was fine. I've implemented what Alexey suggested in his comments. Here's the solution:

pow <- function(x,y)
{
  return(x^y)
}


#### posterior distribution
posteriorDistribution <- function(x, y, a, b,s2,N)
{
sumSqError <- 0.0
for(i in 1:N)
{
  sumSqError <- sumSqError + pow(y[i] - (a + b*x[i]),2)
}
return((-((N/2)+1) * log(s2)) + ((-0.5/s2) * sumSqError))

}

# x <- x values
# y <- actual datapoints
# N <- sample size
# m <- length of chain
# sigmaProposalWidth <- width of uniform proposal dist for sigma squared
# paramAProposalWidth <- width of uniform proposal dist for intercept
# paramBProposalWidth <- width of uniform proposal dist for slope

mcmcSampling <- function(x,y,N,m,sigmaProposalWidth,paramAProposalWidth,paramBProposalWidth)
{

  desiredAcc <- 0.44

  paramsA <- vector("numeric",length=m) # intercept
  paramsB <- vector("numeric",length=m) # slope
  s2 <- vector("numeric",length=m) # sigma squared

  paramsA[1] <- 0
  paramsB[1] <- 0
  s2[1] <- 1

  accATot <- 0
  accBTot <- 0
  accS2Tot <- 0

  for(i in 2:m)
  {
    paramsA[i] <- paramsA[i-1] + runif(1,-paramAProposalWidth,paramAProposalWidth)
    accA <- 1
    if((posteriorDistribution(x,y,paramsA[i],paramsB[i-1],s2[i-1],N) - 
        posteriorDistribution(x,y,paramsA[i-1],paramsB[i-1],s2[i-1],N)) < log(runif(1)))
    {
      paramsA[i] <- paramsA[i-1]
      accA <- 0
    }


    accATot <- accATot + accA

    paramsB[i] <- paramsB[i-1] + runif(1,-paramBProposalWidth,paramBProposalWidth)
    accB <- 1
    if((posteriorDistribution(x,y,paramsA[i],paramsB[i],s2[i-1],N) - 
        posteriorDistribution(x,y,paramsA[i-1],paramsB[i-1],s2[i-1],N)) < log(runif(1)))
    {
      paramsB[i] <- paramsB[i-1]
      accB <- 0
    }

    accBTot <- accBTot + accB

    s2[i] <- s2[i-1] + runif(1,-sigmaProposalWidth,sigmaProposalWidth)
    accS2 <- 1

    if((s2[i] < 0) || (posteriorDistribution(x,y,paramsA[i],paramsB[i],s2[i],N) - 
                       posteriorDistribution(x,y,paramsA[i],paramsB[i],s2[i-1],N)) < log(runif(1)))
    {
      s2[i] <- s2[i-1]
      accS2 <- 0
    }

    accS2Tot <- accS2Tot + accS2

    if(i%%100==0)
    {

      paramAProposalWidth <- paramAProposalWidth * ((accATot/100)/desiredAcc)
      paramBProposalWidth <- paramBProposalWidth * ((accBTot/100)/desiredAcc)
      sigmaProposalWidth <- sigmaProposalWidth * ((accS2Tot/100)/desiredAcc)

      accATot <-  0
      accBTot <-  0 
      accS2Tot <-  0

    }


  }
    res <- data.frame(paramsA,paramsB,s2)
    return(res)

}

相关问答

更多
  • 如果y碰巧为0(并且每次迭代的概率为0.5,这将确定地发生),则alpha为0/0(因为x[i-1] == 0 )。 它给你NaN 。 条件something <= NaN提供NA 。 If y happens to be 0 (and with 0.5 probability for each iteration this will happen with certainty), then alpha is 0 / 0 (because x[i-1] == 0). It gives you NaN. Co ...
  • 您的代码在LinearRegression的构造函数中有错误。 代替: reg = LinearRegression(x,y) 做这个: reg = LinearRegression() 现在至于你所说的错误,这是因为你在X中只有一列。所以当前的形状是 (n_rows,) 所有scikit估计器都需要X的形状: (n_rows, n_columns) 所以,像这样重塑你的X: X = X.reshape(-1,1) 然后将它们传递给fit() Your code has error in the ...
  • 我只是从Spark MLlib开始,特别是线性回归,所以我只能讨论技术问题(不是为什么在机器学习中这样做的原因)。 这是我陷入困境的部分。 如何使用此模型进行预测? 模型是变换器(如VectorAssembler ),它提供了一个与变换运算符非常简单的接口。 transform(dataset:Dataset [_]):DataFrame转换输入数据集。 这就是您传递数据集并返回另一个带有prediction列的数据集的位置。 这就是训练和预测的一般方法。 以下内容将为您提供输入数据集中功能的预测。 val ...
  • 这是一个快速设置,以帮助您入门 from statsmodels.tsa.stattools import ARMA import pandas as pd import numpy as np ts = pd.Series(np.random.randn(500), index=pd.date_range('2010-01-01', periods=500)) p, q = 1, 1 arma = ARMA(endog=ts, order=(p, q)).fit() print arma.summ ...
  • 它有点笨重,但可以将模型导出到PMML文件,重新读入然后使用XPath解析它。 这个链接有一个使用决策树的例子。 It's mildly clunky but it is possible to export the model to a PMML file, read it back in and then parse it using XPath. There's an example using decision trees at this link.
  • 改变这些行 X = np.reshape(X,(5)) Y = np.reshape(Y,(5)) 或者只是删除它们 Change these lines X = np.reshape(X,(5)) Y = np.reshape(Y,(5)) or just removed them both
  • 您正在进行随机梯度下降,评估每个数据点的拟合度。 所以最后的m和c给出了拟合关系的参数。 您绘制的线是拟合线的“演变”。 以下是我绘制它的方式,以及对代码的一些其他调整,因为我弄清楚你在做什么: import numpy as np import matplotlib.pyplot as plt X = np.array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) Y = np.array([ 3, 5, 9, 9, 11, 13, 16, 17, 19, 2 ...
  • 您可以使用bquote对语言进行一些简单的计算来构建您的呼叫。 temp_fm_list = lapply(temp_formula_list, function(x) { lmc <- bquote( lm(data = mtcars, formula = .(x))) eval(lmc) }) temp_fm_list # Call: # lm(formula = hp ~ 1, ...
  • 拦截部分有一个错误(paramsA)。 其他一切都很好。 我已经实施了Alexey在评论中提出的建议。 这是解决方案: pow <- function(x,y) { return(x^y) } #### posterior distribution posteriorDistribution <- function(x, y, a, b,s2,N) { sumSqError <- 0.0 for(i in 1:N) { sumSqError <- sumSqError + pow(y[i] - ...
  • 你有3个问题: 1)您似乎想要模拟二项分布,因此随机游走应该在1:n范围内的整数而不是[0,1]范围内的实数。 2)你在alpha的计算中切换了分子和分母 3)你有一个错误的X[t] < X[t - 1] 。 修复这些并清理你的代码(包括使用dbinom函数@ZheyuanLi建议)产生: metropolisAlgorithm <- function(n, p, T){ # implementation of the algorithm # @n,p binomial parameters # ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。