对不起,我真的试过了,但我找不到这个简单问题的解决方案。
我需要在 plack 中获取 id session,但不是通过浏览器,我想在主命名空间 perl plack 代码中做内部背景。
这是我的代码:
#!/usr/bin/perl
use Plack::Builder;
use YAML;
use Plack::Session::Store::File;
my $app = sub {
my $env = shift;
#$global = 'write thsi text to file';
$global = $env->{'psgix.session.options'}{id};
return [ 200, [], [ "App session ID: " . $global ] ];
};
$app->();
open my $fileHandle, ">>", "file" or die "Can't open '\n";
print $fileHandle $global;
close $fileHandle;
my $id = sub {
my $env = shift;
return [ 200, [], [ 'The ID session is: ' . $global ] ];
};
builder {
enable 'Session', store => Plack::Session::Store::File->new(
dir => './sessiondir',
serializer => sub { YAML::DumpFile( reverse @_ ) },
deserializer => sub { YAML::LoadFile( @_ ) },
);
mount '/id' => $id;
mount '/' => $app;
};
如果我访问根路径http://192.168.1.1:5000,我会得到:
App session ID: 33d2e5c9fc9d57ca79679675130d7a06b7ccc1f6
如果我访问http://192.168.1.1:5000/id
The ID session is: 33d2e5c9fc9d57ca79679675130d7a06b7ccc1f6
我想从 $global 变量的文件中写入 id 会话:
open my $fileHandle, ">>", "file" or die "Can't open '\n";
print $fileHandle $global;
close $fileHandle;
在目标文件中我什么也没得到,但是如果我将变量更改为此,它可以工作:
#$global = 'write thsi text to file';
也许问题是因为这段文字:
线程池中的线程由系统管理。这些线程不绑定到当前请求。因此,Session 对他们不可用。
资料来源: 我可以在后台线程中访问会话吗?
我的目标只是获取 id 并打开此文件以从后台执行其他操作。我想避免将另一个文件保存到这个简单的任务中,避免使用 sql 等。
因为我已经将数据会话保存在一个文件中:
enable 'Session', store => Plack::Session::Store::File->new(
dir => './sessiondir',
serializer => sub { YAML::DumpFile( reverse @_ ) },
deserializer => sub { YAML::LoadFile( @_ ) },
);
我感谢任何建议,也许这可以通过另一种方式实现。
非常感谢。