gpt4 book ai didi

multithreading - Perl getstore在线程中

转载 作者:行者123 更新时间:2023-12-03 12:59:42 24 4
gpt4 key购买 nike

在以下脚本中:

use strict;
use warnings;
use LWP::Simple;
use threads;
threads->create(sub {
my $url = "http://www.example.com/logo.jpg";
my $file = "/var/www/html/logo.jpg";
getstore($url, $file);
threads->detach();
});

当我启动它时,它不会保存图像,但是如果我不在线程中启动相同的代码,它将起作用,为什么?

最佳答案

因为“分离”并没有达到您的期望。当程序退出时,分离的线程将终止。 From the docs...

$thr->detach()

Makes the thread unjoinable, and causes any eventual return value to be discarded. When the program exits, any detached threads that are still running are silently terminated.


您应该已经收到了这样的消息。
Perl exited with active threads:
1 running and unjoined
0 finished and unjoined
0 running and detached
而不是分离,您应该等到程序结束时所有线程都完成。
for my $thread (threads->list(threads::running)) {
$thread->join;
}

如果您只想发出并行HTTP请求,则不需要线程。 LWP::Parallel可能会更有效。

关于multithreading - Perl getstore在线程中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41758080/

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