Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

concurrent processes. 3

Status
Not open for further replies.

JD9955

Programmer
Apr 16, 2007
15
US
Hi All,

I am after a way in which I can run 2 perl programs in parallel..

Currently I have the following code which executes 2 programs consecutively, (not concurrently) ..

While (1 = 1)
{
system "perl Program1.pl" || die "Cannot open";

system "perl Program2.pl" || die "Cannot open";
sleep 20;
}

How can I make ‘Program1’ and ‘Program2’ run in parallel? And taking into account that each program must die after it has finished because of the infinite loop..


Thanks All, :)

Regards,

JD
 
Thanks Travs69,, much appreciated :)

I have the following code:

*****************************************

my $i;

my $pid = fork();
if (not defined $pid) {
print "resources not avilable.\n";
} elsif ($pid == 0) {


$i = 1;
while ($i != 10000)
{
print "A";
$i++;
}


exit(0);
} else {


$i = 1;
while ($i != 10000)
{
print "B";
$i++;
}


}


*****************************************


when i run this code i get a chunk of B's then a chunk of A's then a chunk of B's ... is this proving that the 2 loops are running concurrently? I assume that the chunking characters are because of process scheduling of the system?

Now, if i were to replace

the "A" looping code with << system "perl Program1.pl" || die "Cannot open"; >>

and the the "B" looping code with << system "perl Program2.pl" || die "Cannot open"; >>

can i assume that these processes will be run concurrently? (well at least to the ability of my computers single processor) ?


cheers :)

-JD
 
Try threads

Code:
use threads;
use threads::shared;

$| = 1;

my $thr1 = threads->new (sub {
   for (my $i = 0; $i < 10000; $i++) {
      print "A";
   }
});
my $thr2 = threads->new (sub {
   for (my $i = 0; $i < 10000; $i++) {
      print "B";
   }
});

# rejoin the threads when they finish
$thr1->join;
$thr2->join;

exit;

-------------
Cuvou.com | The NEW Kirsle.net
 
Your code looks right... as for Kirsle's threads I have never used them at all but he usually provides pretty good information!!

If you replace that code with your system calls it should be good to go.

 
thanks all,

the cpu time switching with the threads is much faster than the processes (i.e. fork) ..

much appreciated :)

A + Kirsle..


-JD
 
Hey Guys,

The threads have been working good, however I am trying to get the consoles to be "invisible" when loaded..

I had the following code:

##########################

BEGIN
{
Win32::SetChildShowWindow(0)
if defined &Win32::SetChildShowWindow };


system "perl P1.pl" || die "Cannot open";

system "perl cP2.pl" || die "Cannot open";

##########################



which doesnt show P1 and P2 when they load.. however i needed to use threads to run concurrently, but now the windows show ... :(

when i use threads (and try the BEGIN) as seen here:

##########################


BEGIN
{
Win32::SetChildShowWindow(0)
if defined &Win32::SetChildShowWindow };



$| = 1;

my $thr1 = threads->new (sub {

system "perl P1.pl" || die "Cannot open";

});

my $thr2 = threads->new (sub {

system "perl P2.pl" || die "Cannot open";

});

# rejoin the threads when they finish
$thr1->join;
$thr2->join;

exit;

##########################

After trying the threads and the hide child windows,, they are still shown ..


Any ideas how I can have the best of both worlds ??

Thanks all :)

-JD
 
I forgot to mention , that if its still possible to do this using:

####

BEGIN
{
Win32::SetChildShowWindow(0)
if defined &Win32::SetChildShowWindow };

####

As i have been hitting my head against a wall with Win32::GUI and Win32::API for the last 2 hours..


cheers :)

JD
 
If you're using Windows anyway, run them with wperl instead of perl.

Code:
wperl script.pl

wperl doesn't have a console window by default, and you don't even have to use Win32::GUI modules in your code.

-------------
Cuvou.com | The NEW Kirsle.net
 
Hey Kirsle,

yeah i tried wperl too, but with threads, it opens the command window in which you specify “wperl P1.pl” and the window stays open until the threads finish their jobs. The thread windows also open up until they finish their individual jobs..


This is my layout:

Run.bat (which contains wperl P1.pl )

P1.pl (which contains two threaded system calls to P1a.pl and P1b.pl (as seen in the above code) )

P1a and P1b (are the two programs which are executed as threads )



Hmmmm,, any other ideas ? (such a simple task, yet is driving me up the wall :p )

Cheers,,

-JD
 
Ohh, right... yeah, system() opens cmd windows even if your script doesn't have one...

If you're just running other Perl scripts, why not require or do them instead?

-------------
Cuvou.com | The NEW Kirsle.net
 
Look at Win32:process.

There is a argument which determines whether to wait or to proceed.
 
I just wanted to note that what Kirsle describes worked perfectly. Here is the sample code that I threw together to test it. You run "wperl.exe threads.pl" and the script immediate returns while threads continue until they've both outputted their full results to the file. No additional command windows are opened.

threads.pl:

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]threads[/green][red];[/red]
[black][b]use[/b][/black] [green]threads::shared[/green][red];[/red]

[url=http://perldoc.perl.org/functions/unlink.html][black][b]unlink[/b][/black][/url] [red]'[/red][purple]output.txt[/purple][red]'[/red][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$thr1[/blue] = threads->[maroon]new[/maroon] [red]([/red][url=http://perldoc.perl.org/functions/sub.html][black][b]sub[/b][/black][/url] [red]{[/red]
	[url=http://perldoc.perl.org/functions/do.html][black][b]do[/b][/black][/url] [red]"[/red][purple]a.pl[/purple][red]"[/red][red];[/red]
[red]}[/red][red])[/red][red];[/red]

[black][b]my[/b][/black] [blue]$thr2[/blue] = threads->[maroon]new[/maroon] [red]([/red][black][b]sub[/b][/black] [red]{[/red]
	[black][b]do[/b][/black] [red]"[/red][purple]b.pl[/purple][red]"[/red][red];[/red]
[red]}[/red][red])[/red][red];[/red]

[gray][i]# rejoin the threads when they finish[/i][/gray]
[blue]$thr1[/blue]->[maroon]join[/maroon][red];[/red]
[blue]$thr2[/blue]->[maroon]join[/maroon][red];[/red]

[url=http://perldoc.perl.org/functions/exit.html][black][b]exit[/b][/black][/url][red];[/red]

a.pl:

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]File::Slurp[/green] [red]qw([/red][purple]append_file[/purple][red])[/red][red];[/red]

[maroon]append_file[/maroon][red]([/red][red]'[/red][purple]output.txt[/purple][red]'[/red], [red]"[/red][purple]a[blue]$_[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red])[/red] [olive][b]for[/b][/olive] [red]([/red][fuchsia]0..10000[/fuchsia][red])[/red][red];[/red]

b.pl:

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]File::Slurp[/green] [red]qw([/red][purple]append_file[/purple][red])[/red][red];[/red]

[maroon]append_file[/maroon][red]([/red][red]'[/red][purple]output.txt[/purple][red]'[/red], [red]"[/red][purple]       b[blue]$_[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red])[/red] [olive][b]for[/b][/olive] [red]([/red][fuchsia]0..10000[/fuchsia][red])[/red][red];[/red]

[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]threads - Perl extension allowing use of interpreter based threads from perl[/li]
[li]threads::shared - Perl extension for sharing data structures between threads[/li]
[/ul]
Other Modules used :
[ul]
[li]File::Slurp[/li]
[/ul][/tt]

Kudos Kirsle,

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top