gpt4 book ai didi

multithreading - 如何让 PSGI 程序每个进程而不是每个线程只进行一次昂贵的初始化?

转载 作者:行者123 更新时间:2023-12-03 13:38:06 26 4
gpt4 key购买 nike

转帖:http://perlmonks.org/?node_id=1191821

考虑app.psgi :

#!perl
use 5.024;
use strictures;
use Time::HiRes qw(sleep);

sub mock_connect {
my $how_long_it_takes = 3 + rand;
sleep $how_long_it_takes;
return $how_long_it_takes;
}
sub main {
state $db_handle = mock_connect($dsn);
return sub { [200, [], ["connect took $db_handle seconds\n"]] };
}
my $dsn = 'dbi:blahblah'; # from config file
my $app = main($dsn);

测量 plackup ( HTTP::Server::PSGI: Accepting connections at http://0:5000/):
› perl -MBenchmark=timeit,timestr,:hireswallclock -E"say timestr timeit 10, sub { system q(curl http://localhost:5000) }"
connect took 3.0299610154043 seconds
connect took 3.0299610154043 seconds
connect took 3.0299610154043 seconds
connect took 3.0299610154043 seconds
connect took 3.0299610154043 seconds
connect took 3.0299610154043 seconds
connect took 3.0299610154043 seconds
connect took 3.0299610154043 seconds
connect took 3.0299610154043 seconds
connect took 3.0299610154043 seconds
2.93921 wallclock secs ( 0.03 usr + 0.06 sys = 0.09 CPU) @ 107.53/s (n=10)

测量 thrall ( Starting Thrall/0.0305 (MSWin32) http server listening at port 5000):
› perl -MBenchmark=timeit,timestr,:hireswallclock -E"say timestr timeit 10, sub { system q(curl http://localhost:5000) }"
connect took 3.77111188120125 seconds
connect took 3.15455510265111 seconds
connect took 3.77111188120125 seconds
connect took 3.15455510265111 seconds
connect took 3.77111188120125 seconds
connect took 3.64333342488772 seconds
connect took 3.15455510265111 seconds
connect took 3.77111188120125 seconds
connect took 3.85268922343767 seconds
connect took 3.64333342488772 seconds
17.4764 wallclock secs ( 0.02 usr + 0.09 sys = 0.11 CPU) @ 90.91/s (n=10)

这种性能是 Not Acceptable ,因为初始化发生了多次,尽管 state多变的。你是怎么做到的,所以它只发生一次?

最佳答案

无论出于何种原因,程序 thrall在其配置部分硬编码一个“loader”参数:

my $runner = Plack::Runner->new(
server => 'Thrall',
env => 'deployment',
loader => 'Delayed',
version_cb => \&version,
);

$runner->parse_options(@ARGV);

那个字符串 "Delayed"指模块 Plack::Loader::Delayed ,这会延迟 .psgi 文件的加载,直到第一个请求到来。这将符合您的基准测试结果。 (如果您在不杀死 thrall 的情况下再次运行基准测试,您将看到相同的输出)。

您可以尝试运行 thrall -L +Plack::Loader app.psgi ,它将“loader”参数恢复为硬编码在 Plack::Runner中的默认值.

关于multithreading - 如何让 PSGI 程序每个进程而不是每个线程只进行一次昂贵的初始化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44257530/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com