在后台线程中获取Plack会话ID

时间:2017-11-10 17:03:06

标签: perl session plack psgi

抱歉,我真的尝试过,但我找不到解决这个简单问题的方法。

我需要在plack中获取id会话,但不是通过浏览器,我想在主命名空间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 variable:

的文件中编写id会话
open my $fileHandle, ">>", "file" or die "Can't open '\n";
print $fileHandle $global;
close $fileHandle;

在目标文件中,我什么都没得到,但如果我将变量改为此,则可以正常工作:

#$global = 'write thsi text to file';

也许问题是因为这个文字:

  

线程池中的线程由系统管理。这些线程   不受当前请求的约束。因此,Session不是   可供他们使用。

来源: Can I access sessions in background thread?

我的目标只是获取id并打开此文件以从后台执行其他操作。 我想避免将另一个文件保存到这个简单的任务中,避免使用sql等。

因为我已将数据会话保存在文件中:

enable 'Session', store => Plack::Session::Store::File->new(
                dir => './sessiondir',
                serializer   => sub { YAML::DumpFile( reverse @_ ) },
                deserializer => sub { YAML::LoadFile( @_ ) },
            );

我感谢任何建议,也许这可能是另一种方式。

非常感谢。

0 个答案:

没有答案