[英]Perl script running a periodic (main) task and providing a REST interface
I am working on a Perl script which does some periodic processing based on file-system contents.我正在研究一个 Perl 脚本,该脚本根据文件系统内容进行一些定期处理。 The overall structure is like this:
整体结构是这样的:
# ... initialization...
while(1) {
# ... scan filesystem, perform actions depending on changes detected ...
sleep 5;
}
I would like to add the ability to input some data into this process by means of exposing an interface through HTTP.我想通过 HTTP 暴露一个接口来添加将一些数据输入到这个过程中的能力。 Eg I would like to add an endpoint to skip the sleep, but also some means to input data that is processed in the next iteration.
例如,我想添加一个端点来跳过睡眠,但也想添加一些方法来输入在下一次迭代中处理的数据。 Additionally, I would like to be able to query some of the program's status through HTTP (ie a simple
fork()
to run the webserver-part in a separate process is insufficient?)另外,我希望能够通过 HTTP 查询一些程序的状态(即一个简单的
fork()
在单独的进程中运行 webserver-part 是不够的?)
So far I have already used the Dancer2
framework once but it has a start;
到目前为止,我已经使用过一次
Dancer2
框架,但它有一个start;
call that blocks and thus does not allow any other tasks (like my loop) to run.调用该块,因此不允许任何其他任务(如我的循环)运行。 Additionally, I could of course move the code which is currently inside the loop to an endpoint exposed through
Dancer2
but then I would need to call that periodically (though an external program?) which seems to be quite an obscure indirection compared to just having the webserver-part running in background.此外,我当然可以将当前在循环内的代码移动到通过
Dancer2
暴露的端点,但随后我需要定期调用它(虽然是外部程序?),与仅具有webserver-part 在后台运行。
Is it possible to unobtrusively (ie without blocking the program) add a REST-server capability to a Perl script?是否可以不显眼地(即不阻止程序)向 Perl 脚本添加 REST 服务器功能? If yes: Which modules would be used for the purpose?
如果是:哪些模块将用于此目的? If no: Should I really implement an external process to periodically invoke a certain endpoint or pursue a different solution altogether?
如果不是:我真的应该实现一个外部流程来定期调用某个端点还是完全追求不同的解决方案?
(I have tried to add a dancer2
tag, but could not do so due to insufficient reputation. Do not be mislead by this: I have so far only tried with Dancer2
not the Dancer
(v.1)) (我曾尝试添加
dancer2
标签,但由于声誉不足而无法添加。不要被此误导:到目前为止,我只尝试过使用Dancer2
而不是Dancer
(v.1))
You could try to launch your processing loop in a background thread, before you run start;
在运行
start;
. .
See man perlthrtut
见
man perlthrtut
You probably want use threads::shared;
您可能想要
use threads::shared;
to declare some variables shared between the REST part and the background thread.声明 REST 部分和后台线程之间共享的一些变量。 Or use dedicated queues/event mechanisms.
或使用专用队列/事件机制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.