[27142] in Perl-Users-Digest
Perl-Users Digest, Issue: 8993 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Feb 25 09:05:54 2006
Date: Sat, 25 Feb 2006 06:05:03 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Sat, 25 Feb 2006 Volume: 10 Number: 8993
Today's topics:
Re: Merging sparse arrays robic0
Re: sharing variables-data perl-asp <jay@cutmeukjay.com>
Re: Threads <zentara@highstream.net>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 24 Feb 2006 18:06:41 -0800
From: robic0
Subject: Re: Merging sparse arrays
Message-Id: <llevv15qbgbg92c2qgs93br3tlvjr233pi@4ax.com>
On 16 Feb 2006 07:24:37 GMT, Abigail <abigail@abigail.nl> wrote:
>Samwyse (samwyse@gmail.com) wrote on MMMMDLII September MCMXCIII in
><URL:news:0ZQIf.48417$dW3.26427@newssvr21.news.prodigy.com>:
>^^ Abigail wrote:
snipper
>Defined-or will be in 5.10, is currently in 5.9.3, and there's a patch
>available for 5.8.x.
Patches, defined in 5.10, 5.9.3, available for 5.8.x<--wtf is that?
Alot of corporations use 5.8.x<--, can we be assured you know what
your talking about Abby? As far as i see your blowing sunshine up
alot of buts here.
Explain in detail what your talking about for the implementors......
>
>
>
>Abigail
------------------------------
Date: Sat, 25 Feb 2006 06:25:48 -0000
From: "UkJay" <jay@cutmeukjay.com>
Subject: Re: sharing variables-data perl-asp
Message-Id: <dtot97$kf0$1@newsg3.svr.pol.co.uk>
Keywords: perl tuition
"Matt Garrish" <matthew.garrish@sympatico.ca> wrote in message
news:r5NLf.26070$%14.676402@news20.bellglobal.com...
>
<snip>
<ouch>
> Why do you want to shell out to perl when you're already running perl?
> PerlScript is Perl; just running within the asp environment. That said,
> there's no reason you can't also shell out to another script using
> backticks (but you're going to pay the overhead of starting another perl
> interpreter each time):
>
> my $returnValues = `perl somescript.pl`;
>
> Note that I called perl explicitly. I never looked into this too deeply on
> Windows, but have always just assumed that the Windows extension mappings
> are not available in the shell that is created to execute the command. If
> you don't call perl, don't expect your script to run.
>
> The data sharing is certainly not impossible, but not terribly nice
> either. Options that spring to mind are to print the variables you want
> back to stdout as a string in some delimited fashion and then split that
> string from your within your asp script. Or you could go the more powerful
> route and use the Storable module to write the data structures to file and
> then send that filename back to your script to read them back in.
>
> You may find yourself limited in terms of what you can run and do from a
> shell, though. You will be running with limited rights no matter what
> server you're using.
>
> In other words, reconsider why you want to write a separate script!
>
> Matt
>
Thanks for your reply Matt
my $returnValues = `perl somescript.pl`; seems to be what I need
but I do take the point that normally I wouldn't need to shell out to perl.
--
Best Regards,
James (ukjay)
http://www.ukjay.co.uk
Garden WebCam,Photography,Competitions,Weather (AWS)
------------------------------
Date: Sat, 25 Feb 2006 13:58:21 GMT
From: zentara <zentara@highstream.net>
Subject: Re: Threads
Message-Id: <gam0029gnfjt4mgbjlfde55qnf8s3fu071@4ax.com>
On Fri, 24 Feb 2006 14:47:49 +0100, RBCS <rbcs@gmx.net> wrote:
>Can someone tell me which module I should use to create tasks in windows
>where I can run certain work in it. I used POE module, but this library
>crashes when I load it with a lot of work and is not very happy with
>ActiveState perl.
>
>What do you recomend me?
>Roman
I use linux, but, here is one option for you to consider.
Everything is a thread on win32, so you could use threads.
The only reason you would need to use threads, rather than
a separate process, is if you want to share data between your
threads and the main program. Otherwise you could just use
Win32::Process to spawn off your work.
The thing about threads, is that in general( to be foolproof :-) ),
you need to create the threads before you create any objects
in your main thread. This usually means creating a bank of threads
at the beginning of the program, and putting them to sleep.
You then wake them up, and feed them data, and let them work,
let them go back to sleep when done.
You can create objects in the thread code block, which will only
be used in the thread, like LWP objects for net work.
You can create them dynamically, or use Thread::Queue, but
that is a little trickier.
Here is an example using Tk, you can rip the Tk code out, to
just get the basic thread code, but Tk gives an event-loop
style, like POE.
In the following example, I send the thread to the end of it's code
block, using an goto statement( for various reasons ), but you could
just as well use a return. For a thread to be properly terminated, it
must reach the end of it's code block( return), and then be joined
with the join command.
You can share data with the thread 2 ways.
1, thru shared variables, OR
2, you can pass it args on creation ( like passing @_ to a sub),
and get returned data when it is joined.
( like the return values from a sub)
There are tons of examples for threads on groups.google.com.
You really should say something about what sort of tasks you
want to run in the threads, because there are many different
styles and queue's which you can use.
This runs on Windows. I have noticed that threads work alot better
on WindowsXP, than on older versions like win95, or winME.
Earlier windows versions bog down when there are huge numbers
of shared variables.
This script runs a bank of 3 worker threads. It passes some code
to be run (example: date), then starts a counter.
#!/usr/bin/perl
use warnings;
use strict;
use threads;
use threads::shared;
#works on Windows
my $data = shift || 'date'; #sample code to pass to thread
# create the threads before anything is done in main
# to avoid object thread safety problems
my %shash;
#share(%shash); #will work only for first level keys
my %hash;
my %workers;
my $numworkers = 3;
foreach my $dthread(1..$numworkers){
share ($shash{$dthread}{'go'});
share ($shash{$dthread}{'progress'});
share ($shash{$dthread}{'timekey'}); #actual instance of the thread
share ($shash{$dthread}{'frame_open'}); #open or close the frame
share ($shash{$dthread}{'handle'});
share ($shash{$dthread}{'data'});
share ($shash{$dthread}{'pid'});
share ($shash{$dthread}{'die'});
$shash{$dthread}{'go'} = 0;
$shash{$dthread}{'progress'} = 0;
$shash{$dthread}{'timekey'} = 0;
$shash{$dthread}{'frame_open'} = 0;
$shash{$dthread}{'handle'} = 0;
$shash{$dthread}{'data'} = $data;
$shash{$dthread}{'pid'} = -1;
$shash{$dthread}{'die'} = 0;
$hash{$dthread}{'thread'} = threads->new(\&work,$dthread);
}
use Tk;
use Tk::Dialog;
my $mw = MainWindow->new(-background => 'gray50');
my $lframe = $mw->Frame( -background => 'gray50',-borderwidth=>10 )
->pack(-side =>'left' ,-fill=>'y');
my $rframe = $mw->Frame( -background => 'gray50',-borderwidth=>10 )
->pack(-side =>'right',-fill =>'both' );
my %actives = (); #hash to hold reusable numbered widgets used for
downloads
my @ready = (); #array to hold markers indicating activity is needed
#make 3 reusable downloader widget sets-------------------------
foreach(1..$numworkers){
push @ready, $_;
#frames to hold indicator
$actives{$_}{'frame'} = $rframe->Frame( -background => 'gray50' );
$actives{$_}{'stopbut'} = $actives{$_}{'frame'}->Button(
-text => "Stop Worker $_",
-background => 'lightyellow',
-command => sub { } )->pack( -side => 'left', -padx => 10 );
$actives{$_}{'label1'} = $actives{$_}{'frame'} ->Label(
-width => 3,
-background => 'black',
-foreground => 'lightgreen',
-textvariable => \$shash{$_}{'progress'},
)->pack( -side => 'left' );
$actives{$_}{'label2'} = $actives{$_}{'frame'} ->Label(
-width => 1,
-text => '%',
-background => 'black',
-foreground => 'lightgreen',
)->pack( -side => 'left' );
$actives{$_}{'label3'} = $actives{$_}{'frame'} ->Label(
-text => '',
-background => 'black',
-foreground => 'skyblue',
)->pack( -side => 'left',-padx =>10 );
}
#--------------------------------------------------
my $button = $lframe->Button(
-text => 'Get a worker',
-background => 'lightgreen',
-command => sub { &get_a_worker(time) }
)->pack( -side => 'top', -anchor => 'n', -fill=>'x', -pady =>
20 );
my $text = $rframe->Scrolled("Text",
-scrollbars => 'ose',
-background => 'black',
-foreground => 'lightskyblue',
)->pack(-side =>'top', -anchor =>'n');
my $repeat;
my $startbut;
my $repeaton = 0;
$startbut = $lframe->Button(
-text => 'Start Test Count',
-background => 'hotpink',
-command => sub {
my $count = 0;
$startbut->configure( -state => 'disabled' );
$repeat = $mw->repeat(
100,
sub {
$count++;
$text->insert( 'end', "$count\n" );
$text->see('end');
}
);
$repeaton = 1;
})->pack( -side => 'top', -fill=>'x', -pady => 20);
my $stoptbut = $lframe->Button(
-text => 'Stop Count',
-command => sub {
$repeat->cancel;
$repeaton = 0;
$startbut->configure( -state => 'normal' );
})->pack( -side => 'top',-anchor => 'n', -fill=>'x', -pady => 20 );
my $exitbut = $lframe->Button(
-text => 'Exit',
-command => sub {
foreach my $dthread(keys %hash){
$shash{$dthread}{'die'} = 1;
$hash{$dthread}{'thread'}->join
}
if ($repeaton) { $repeat->cancel }
#foreach ( keys %downloads ) {
# #$downloads{$_}{'repeater'}->cancel;
#}
# $mw->destroy;
exit;
})->pack( -side => 'top',-anchor => 'n', -fill=>'x', -pady => 20
);
#dialog to get file url---------------------
my $dialog = $mw->Dialog(
-background => 'lightyellow',
-title => 'Get File',
-buttons => [ "OK", "Cancel" ]
);
my $hostl = $dialog->add(
'Label',
-text => 'Enter File Url',
-background => 'lightyellow'
)->pack();
my $hostd = $dialog->add(
'Entry',
-width => 100,
-textvariable => '',
-background => 'white'
)->pack();
$dialog->bind( '<Any-Enter>' => sub { $hostd->Tk::focus } );
my $message = $mw->Dialog(
-background => 'lightyellow',
-title => 'ERROR',
-buttons => [ "OK" ]
);
my $messagel = $message->add(
'Label',
-text => ' ',
-background => 'hotpink'
)->pack();
$mw->repeat(10, sub{
if(scalar @ready == $numworkers){return}
foreach my $set(1..$numworkers){
$actives{$set}{'label1'}->
configure(-text =>\$shash{$set}{'progress'});
if(($shash{$set}{'go'} == 0) and
($shash{$set}{'frame_open'} == 1))
{
my $timekey = $shash{$set}{'timekey'};
$workers{ $timekey }{'frame'}->packForget;
$shash{$set}{'frame_open'} = 0;
push @ready, $workers{$timekey}{'setnum'};
if((scalar @ready) == 3)
{ }
$workers{$timekey} = ();
delete $workers{$timekey};
}
}
});
$mw->MainLoop;
###################################################################
sub get_a_worker {
my $timekey = shift;
$hostd->configure( -textvariable => \$data);
if ( $dialog->Show() eq 'Cancel' ) { return }
#----------------------------------------------
#get an available frameset
my $setnum;
if($setnum = shift @ready){print "setnum->$setnum\n"}
else{ print "no setnum available\n"; return}
$workers{$timekey}{'setnum'} = $setnum;
$shash{$setnum}{'timekey'} = $timekey;
$workers{$timekey}{'frame'} = $actives{$setnum}{'frame'};
$workers{$timekey}{'frame'}->pack(-side =>'bottom', -fill => 'both' );
$workers{$timekey}{'stopbut'} = $actives{$setnum}{'stopbut'};
$workers{$timekey}{'stopbut'}->configure(
-command => sub {
$workers{$timekey}{'frame'}->packForget;
$shash{ $workers{$timekey}{'setnum'} }{'go'} = 0;
$shash{ $workers{$timekey}{'setnum'} }{'frame_open'} = 0;
push @ready, $workers{$timekey}{'setnum'};
if((scalar @ready) == $numworkers)
{ }
$workers{$timekey} = ();
delete $workers{$timekey};
});
$workers{$timekey}{'label1'} = $actives{$setnum}{'label1'};
$workers{$timekey}{'label1'}->configure(
-textvariable => \$shash{$setnum}{'progress'},
);
$workers{$timekey}{'label2'} = $actives{$setnum}{'label2'};
$workers{$timekey}{'label3'} = $actives{$setnum}{'label3'};
$workers{$timekey}{'label3'}->configure(-text => $timekey);
$shash{$setnum}{'go'} = 1;
$shash{$setnum}{'frame_open'} = 1;
#--------end of get_file sub--------------------------
}
##################################################################
sub work{
my $dthread = shift;
$|++;
while(1){
if($shash{$dthread}{'die'} == 1){ goto END };
if ( $shash{$dthread}{'go'} == 1 ){
eval( system( $shash{$dthread}{'data'} ) );
foreach my $num (1..100){
$shash{$dthread}{'progress'} = $num;
print "\t" x $dthread,"$dthread->$num\n";
select(undef,undef,undef, .5);
if($shash{$dthread}{'go'} == 0){last}
if($shash{$dthread}{'die'} == 1){ goto END };
}
$shash{$dthread}{'go'} = 0; #turn off self before returning
}else
{ sleep 1 }
}
END:
}
#####################################################################
--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 8993
***************************************