WWW :: Mechanize :: Mojolicious Lite脚本中的PhantomJS代码在后台模式下运行时无法运行

时间:2015-12-11 23:49:43

标签: perl phantomjs mojolicious-lite

我有这个非常简单的Mojolicious Lite脚本:

#!/usr/bin/env perl

use v5.10;
use WWW::Mechanize::PhantomJS;
use Mojolicious::Lite;

my $mech = WWW::Mechanize::PhantomJS->new();

$mech->get('http://stackoverflow.com/');

get '/test' => sub {
    my $c = shift;
    $mech->get("https://stackoverflow.com/questions");
    $c->render(template => 'activity');
};

app->secrets(['test secret']);
app->start;


__DATA__

@@ activity.html.ep
<!DOCTYPE html>
<html>
  <head><title>Test</title></head>
  <body><h2>Test</h2></body>
</html>

当我在前台模式(hypnotoad -f ./script.pl)中使用hypnotoad启动它,并访问/test url时 - 我得到了我的测试页,并清理了日志:

[Fri Dec 11 18:00:23 2015] [info] Listening at "http://*:8080"
[Fri Dec 11 18:00:23 2015] [info] Manager 3011 started
[Fri Dec 11 18:00:23 2015] [info] Creating process id file "/home/username/pc_activity/demo_site/hypnotoad.pid"

当我使用后台模式(hypnotoad ./script.pl)启动它时,访问/test网址 - 我得到了#34;出了点问题&#34;抛出猛禽的错误页面。

[Fri Dec 11 17:58:07 2015] [info] Listening at "http://*:8080"
[Fri Dec 11 17:58:07 2015] [info] Manager 2964 started
[Fri Dec 11 17:58:07 2015] [info] Creating process id file "/home/username/pc_activity/demo_site/hypnotoad.pid"
[Fri Dec 11 17:58:14 2015] [error] Error while executing command: get: Server returned error message Can't connect to localhost:8910

Connection refused at /usr/local/share/perl/5.18.2/LWP/Protocol/http.pm line 47, <DATA> line 49.
 instead of data at /usr/local/share/perl/5.18.2/Selenium/Remote/Driver.pm line 310.

事实证明,localhost:8910是PhanomJS在webdriver模式下运行的默认设置。它不在我的机器上。但即使我启动它,然后访问URL,我仍然会收到错误:

[Fri Dec 11 18:41:01 2015] [error] Error while executing command: get: Server returned error message Variable Resource Not Found - {"headers":{"Accept":"application/json","Connection":"TE, close","Content-Length":"45","Content-Type":"application/json; charset=utf-8","Host":"localhost:8910","TE":"deflate,gzip;q=0.3","User-Agent":"libwww-perl/6.15"},"httpVersion":"1.1","method":"POST","post":"{\"url\":\"https://stackoverflow.com/questions\"}","url":"/session/98799e80-a060-11e5-8907-0b365878087d/url","urlParsed":{"anchor":"","query":"","file":"url","directory":"/session/98799e80-a060-11e5-8907-0b365878087d/","path":"/session/98799e80-a060-11e5-8907-0b365878087d/url","relative":"/session/98799e80-a060-11e5-8907-0b365878087d/url","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/session/98799e80-a060-11e5-8907-0b365878087d/url","queryKey":{},"chunks":["session","98799e80-a060-11e5-8907-0b365878087d","url"]}} instead of data at /usr/local/share/perl/5.18.2/Selenium/Remote/Driver.pm line 310.

我想,我不明白,为什么它在前台模式下运行,而不是在后台模式下运行。然后,我该怎么做才能让它在后台模式下运行?

1 个答案:

答案 0 :(得分:1)

它不能在后台模式下运行的原因是因为线程。在后台启动hypnotoad后,它会生成一些进程。

 664161507 46028     1   0 10:34AM ??         0:00.01 script.pl
 664161507 46029 46028   0 10:34AM ??         0:00.08 script.pl
 664161507 46030 46028   0 10:34AM ??         0:00.01 script.pl
 664161507 46031 46028   0 10:34AM ??         0:00.01 script.pl
 664161507 46032 46028   0 10:34AM ??         0:00.01 script.pl

这些将无法访问您在父进程中创建的PhantomJS。我没有看到这种沟通是如何完成的,但是如果你想为你的工人分享PhantomJS,你需要把它作为一个单独的服务。

如果你想为每个请求设置一个PhantomJS,你可以在请求中初始化它,但我不一定鼓励这种方法:

 get '/test' => sub {
     my $c = shift;
     my $mech = WWW::Mechanize::PhantomJS->new();
     $mech->get('http://stackoverflow.com/');
     $mech->get("https://stackoverflow.com/questions");
     $c->render(template => 'activity');
 };
相关问题