Hello all, I would like to ask for some help, if you don't mind :)
Anyway for my problem, I'm currently building a script that should be capable of doing a simultaneous request to a certain server. Just like 10 individuals accessing a specific site at the same time.
Im using thread, Thread::Pool module for that matter, WWW::Mechanize to do the requesting stuff. I don't like to destroy the session of every request, that's why I created a loop that runs all throughout until interrupted. So basically every thread keeps on throttling until interrupted.
Example code:
#!/usr/bin/perl -w
use strict;
use Thread::Pool;
use WWW:Mechanize;
my %check :shared = ('success', 0, 'fail', 0, 'exit', 0);
my $pool = Thread::Pool->new(
{
optimize => 'memory',
workers => 1,
do => \&request,
}
);
for(1..10){
$pool->job();
$pool->add;
{
print "Please type 'exit' to quit: ";
chomp ($_=<STDIN>);
if($_=~/exit/i){
$check{exit} = 1;
$pool->shutdown;}
print "success: $check{success}\tfailed: $check{fail}\n";
sub request{
my $flag = 0;
my $mech = WWW::Mechanize->new(autocheck => 1);
$mech->get('http://example_server.com/login.php');
$mech->submit_form(
form_number =>1,
fields =>{
username => 'testuser'
password => '123qwe'
}
);
if($mech->uri() =~/home/i){ #check if login if successful
$check{success}++;
$flag = 1; } else { $check{fail}++;}
while($check{exit} != 1 && $flag == 1){
$mech->follow_link( url_regex => qr/nextpage/i);
$mech->get('http://example_server.com/home');
}
}
Question:
- Is threading the appropriate tool to use or should I use fork instead?
- Just an example, if the server's maximum number of simultaneous requests is set to 10, If the script is not interrupted I should not be able to access the server using my browser right? if the code is correct.
- Memory wise, is there any way I can alleviate the memory usage?
-= thanks for your help...
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.