locked0wn has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks,

I come to you to learn wisdom about HTTP::Proxy
I am trying to use it with threads and even the simplist of code spits out information that makes me question if it is thread safe. Everything is working fine, but the window I am running my script receives "exit status information" everytime something is performed. If someone tells me this is normal behavior for HTTP::Proxy when using threads, I only ask to know why?

Here is the code I used:
use strict; use warnings; use HTTP::Proxy; use threads; #Let's define the threads to be undef my $thr1 = undef; my $thr2 = undef; #Control Server my $Server_IP = "192.168.0.1"; my $Server_Port = 30000; $thr1 = threads->create(\&StartServer_HTTP,1); $thr2 = threads->create(\&StartServer_HTTP,2); $thr1->join(); $thr2->join(); sub StartServer_HTTP{ use HTTP::Proxy; my $thid = threads->tid(); my $port = $Server_Port + $thid; print STDOUT "Server started on port: <$port>\n"; my $proxy = HTTP::Proxy->new( host => $Server_IP, max_client => 100, max_keep_alive_requests => 1, port => $port ); $proxy->start; }

When I run the code all seems fine. I mean the system is identifying two running proxies, but when I use one of them (setup browser to use proxy on port 30001 and point to a page), I receive a bunch of the following from the window I started the script.
Server started on port: <30001> Server started on port: <30002> Perl exited with active threads: 1 running and unjoined 0 finished and unjoined 0 running and detached Perl exited with active threads: 1 running and unjoined 0 finished and unjoined 0 running and detached Perl exited with active threads: 1 running and unjoined 0 finished and unjoined 0 running and detached

The 'Perl exited with active threads:' and the lines that follow each one of these lines is an exact one-to-one ratio with the pages being pulled back through the proxy. It would be most appreciated if someone could explain this to me and if it is safe to use. You wisdom on this matter is humbling.

Replies are listed 'Best First'.
Re: Is HTTP::Proxy thread safe?
by BrowserUk (Patriarch) on Apr 04, 2010 at 06:16 UTC

    You should read the documentation (their emphasis):

    An important thing to note is that the proxy is ... a forking proxy:

    Update: I just realised that there is no mention of which OS you're running under, so the Win32 explanation might not be the right one for you. I think the solution remains the same though.

    It forks for each new connection. Under Win32, fork is emulated using threads. That's where your "extra' threads come from.

    If you want to run two forking proxies concurrently on different ports, you should probably run two separate processes per the synopsis.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Thank you very much! I agree that I should run two separate processes for two proxies.

      I am running the code I provided on Debian Linux.