我有一个rest服务器写在 perl dancer在虚拟机上,这是工作流程:
- 客户发布了一些json
/post_wait
路由中的内容:POST 应该等待业务处理返回结果 - 请求由另一个虚拟机处理(通信由 POE + IKC 处理)
- 报告应通过
/report
路由返回给REST
服务器通过 session ID 识别的客户
舞者代码:(这是我到目前为止尝试过的)
package var;
use Data::Dumper;
use Dancer ':syntax';
use JSON::XS;
my $session = {};
# curl -H 'Content-Type: application/json' -X POST -d '{"id":22}' http://127.0.0.1:3000/post_wait
post '/post_wait' => sub {
my $json = request->body;
my $h = decode_json $json;
my $id = $h->{id};
until (exists $session->{$id}->{report}) {
sleep 1;
print STDERR ".";
}
return Dumper $session;
};
# curl -H 'Content-Type: application/json' -X POST -d '{"foobar":"xxxx"}' http://127.0.0.1:3000/report
post '/report' => sub {
my $json = request->body;
my $h = decode_json $json;
my $id = $h->{id};
$session->{$id}->{report} = $h;
return "OK\n";
};
true;
问题是此代码挂起,因为我使父进程休眠。
也许我应该使用:
- 用 POE-Component-Server-REST 重写它?
- Dancer::Plugin::Async模块 ?但我可以避免使用这个吗? (有一些 technical restrictions )...
- 另一个脚本作为子进程?
- 类似 redis 的数据库使用
BLPOP
进行队列出队? (需要一些子流程代码) - Dancer::Session ?
fork()
?- 线程? (我猜不会)
我的信息系统应该是可扩展的并且在 high-availability模式。
做我想做的事情的最佳实践是什么?
最佳答案
你需要某种视觉追踪。为此使用 Dancer::Session。
你需要做一些“异步”,可能是 fork()。
然后您可以通过调用来启动业务流程
# curl -H 'Content-Type: application/json' -X POST -d '{"id":22}' http://127.0.0.1:3000/post_wait
进程被 fork 并且 pid 存储在 session 中。
然后你就可以打电话了
# curl -H 'Content-Type: application/json' -X POST -d '{"foobar":"xxxx"}' http://127.0.0.1:3000/report
从 session 中获取 pid(如果有)并检查进程是否仍在运行。
关于perl - 舞者: how to share data and waiting response between 2 POST without blocking,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14042330/