>> From: Rob Coops [mailto:rco...@gmail.com]
>> Sent: Thursday, July 26, 2012 5:26 AM
>> To: beginners@perl.org
>> Subject: Re: Multiprocessing script
>> 
>> On Thu, Jul 26, 2012 at 11:01 AM, Shlomi Fish
>> <shlo...@shlomifish.org>wrote:
>> 
>> > Hi Punit,
>> >
>> > a few comments on your code.
>> >
>> > On Thu, 26 Jul 2012 10:17:13 +0530
>> > punit jain <contactpunitj...@gmail.com> wrote:
>> >
> > >
> > > This is what I get output : -
> > >
> > > perl mprocess.pl /tmp/list1
> > > value of Stop Flag in parent mprocess.pl is 0
> > > value of Stop Flag in child 0 is 0
> > > ** te...@test.com started, pid: 21777 **
> > > value of Stop Flag in parent mprocess.pl is 0
> > > value of Stop Flag in child 0 is 0
> > > ** te...@test.com started, pid: 21778 **
> > > value of Stop Flag in parent mprocess.pl is 0
> > > ** te...@test.com just got out of the pool ** with PID 21777 and parent
> > pid
> > > as 21776 exit code: 0
> > > ** te...@test.com just got out of the pool ** with PID 21778 and parent
> > pid
> > > as 21776 exit code: 0
> > > value of Stop Flag in child 0 is 0
> > > ** te...@test.com started, pid: 21811 **
> > > value of Stop Flag in parent mprocess.pl is 0
> > > value of Stop Flag in child 0 is 0
> > > ** te...@test.com started, pid: 21812 **
> > > value of Stop Flag in parent mprocess.pl is 0
> > > ** te...@test.com just got out of the pool ** with PID 21811 and parent
> > pid
> > > as 21776 exit code: 0
> > > ** te...@test.com just got out of the pool ** with PID 21812 and parent
> > pid
> > > as 21776 exit code: 0
> > > value of Stop Flag in child 0 is 0
> > > ** te...@test.com started, pid: 21832 **
> > > value of Stop Flag in parent mprocess.pl is 0
> > > value of Stop Flag in child 0 is 0
> > > ** te...@test.com started, pid: 21833 **
> > > value of Stop Flag in parent mprocess.pl is 0
> > > ** te...@test.com just got out of the pool ** with PID 21832 and parent
> > pid
> > > as 21776 exit code: 0
> > > ** te...@test.com just got out of the pool ** with PID 21833 and parent
> > pid
> > > as 21776 exit code: 0
> > > value of Stop Flag in child 0 is 1
> > > ** te...@test.com started, pid: 22030 **
> > > value of Stop Flag in parent mprocess.pl is 1
> > > stop flag has been set
> > > Waiting for all children to exit
> > > ** te...@test.com just got out of the pool ** with PID 22030 and parent
> > pid
> > > as 21776 exit code: 0
> > > All children completed
> > >
> > > The concern here is I see value of stopflag as 1 for child before the
> > > parent which is a bit wierd:-
> > >
> > > value of Stop Flag in child 0 is 1
> > > ** te...@test.com started, pid: 22030 **
> > > value of Stop Flag in parent mprocess.pl is 1
> > >
> > > Thanks and Regards.
> >
> >
> Though I cannot fault Shlomi on his style advice I don't think this is what
> you where looking for :-)
> Shlomi is quite right to point out the fact that your code looks like it
> was done in a hurry and is certainly not production quality and could do
> with some improvements. The good thing is that Shlomi is besides being a
> stickler for style and good code also a quite good perl coder and I know
> that if he did find fault in your code he would have pointed it out.
> 
> As for me I tend to vary in style and code cleanliness myself depending on
> the goal and the iteration. :-) Your main question is if the code you wrote
> is done well or if it could be done better. Personally I can't see anything
> terribly wrong with it except for the style and total lack of comments
> (these are pointless for you but the next guy that comes along and looks at
> your code will be thankful for the few moments you spend on that).
> Your other question about the order in which the variables change this is
> not to strange, it might actually not be the order in which the variables
> change. The only thing you see is the order in which the output is pushed
> to the buffer it might very well be that process A simply didn't yield yet
> and managed to push its string to the buffer before the other process got
> the chance to do so.
> As long as the code works correctly and the variables change as expected
> (maybe not in the right order in the logs) you should not worry to much
> about the order in which this shows up in the logs.
> 

When you set up multiple processes or threads, Perl no longer has full control 
of their execution. The OS manages the scheduling and size of the time slices. 
It is very common to see race conditions like this where one process 
occasionally jumps ahead of another. Another symptom is split messages where 
one thread inserts its output in the middle of another's. If you need to manage 
the sequence of events between processes, you will need to look at IPC 
(Inter-Process Communications) capabilities of your platform. Semaphores are 
one mechanism that can be used to control the output sequence.

Bob McConnell

Reply via email to