dongmei2956 2011-07-02 12:27
浏览 44

如何使用perl运行相同的PHP脚本并行?

Ik have a php script that grabs content from another website. I want that same script to run at the same time, so parallel (with different input parameters).

There is a function pcntl that can be used to multithread processes within php, however the manual says it is not recommended to use it in a web server environment. So I decided not to do that since my script is running on a server.

I decided to use threading in perl to call the php function. When I call two different php scripts from Perl they are executed simultaneously, but when I use the same php script the first script has to finish before the second script can start.

It also seems to make no difference if I use thread in perl or not because when using the same script the first session has to finish before it will start with the next. The php scripts are also executed simultaneously without using threads, see script below.

Any help would be very very welcome!

File---> test.pl



#!C:/wamp/bin/perl/bin/perl.exe

print "Content-type: text/html "; print '' . "

"; print '' . "

";

use Config; $Config{useithreads} or die('Recompile Perl with threads to run this program.');

use threads; my $Param3 = 'foo'; my $thr1 = threads->create(\&sub1, 'Param 1', 'Param 2', $Param3); my @ParamList = (42, 'Hello', 3.14); my $thr2 = threads->create(\&sub1, @ParamList); my $thr3 = threads->create(\&sub1, qw(Param1 Param2 Param3)); sub sub1 { my @InboundParameters = @_; print("In the thread "); print '

' . print('Got parameters >', join('<>', @InboundParameters), "< ");}

test1.php:

<?php echo "PHP generated this, this is script test1"; sleep(5); ?>

test2.php:

<?php echo "PHP generated this, this is script test2"; sleep(5);?></pre></code>
  • 写回答

1条回答 默认 最新

  • duanfu1942 2015-02-07 16:53
    关注

    There is a very real danger when mixing and matching in threads, where you can trip over all sorts of 'non thread safe' behavior.

    However, perl is perfectly capable of dealing with parallel stuff without needing to 'call out' to PHP.

    E.g. referring to: Perl daemonize with child daemons

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    use threads;
    use Thread::Queue;
    
    my $nthreads = 5;
    
    my $process_q = Thread::Queue->new();
    my $failed_q  = Thread::Queue->new();
    
    #this is a subroutine, but that runs 'as a thread'.
    #when it starts, it inherits the program state 'as is'. E.g.
    #the variable declarations above all apply - but changes to
    #values within the program are 'thread local' unless the
    #variable is defined as 'shared'.
    #Behind the scenes - Thread::Queue are 'shared' arrays.
    
    sub worker {
    
        #NB - this will sit a loop indefinitely, until you close the queue.
        #using $process_q -> end
        #we do this once we've queued all the things we want to process
        #and the sub completes and exits neatly.
        #however if you _don't_ end it, this will sit waiting forever.
        while ( my $server = $process_q->dequeue() ) {
            chomp($server);
            print threads->self()->tid() . ": pinging $server
    ";
            my $result = `/bin/ping -c 1 $server`;
            if ($?) { $failed_q->enqueue($server) }
            print $result;
        }
    }
    
    #insert tasks into thread queue.
    open( my $input_fh, "<", "server_list" ) or die $!;
    $process_q->enqueue(<$input_fh>);
    close($input_fh);
    
    #we 'end' process_q  - when we do, no more items may be inserted,
    #and 'dequeue' returns 'undefined' when the queue is emptied.
    #this means our worker threads (in their 'while' loop) will then exit.
    $process_q->end();
    
    #start some threads
    for ( 1 .. $nthreads ) {
        threads->create( \&worker );
    }
    
    #Wait for threads to all finish processing.
    foreach my $thr ( threads->list() ) {
        $thr->join();
    }
    
    #collate results. ('synchronise' operation)
    while ( my $server = $failed_q->dequeue_nb() ) {
        print "$server failed to ping
    ";
    }
    

    You can hopefully see that by doing this - you can 'queue up' a list of URLs, then fetch it via LWP within the worker thread. Or if you really must, embed your PHP there.

    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图