in reply to Re^3: Multiprocess - child process cannot be finished successfully
in thread Multiprocess - child process cannot be finished successfully
close($fh); close($logfile); exit; # exit child process } waitpid($pid,0); } print "Parent Process"; close($fh); # Close file handler $fh close($logfile); # Close file handler $logfile rename ($filename, $newfilename) or die "Error in renaming $!"; # Rena +me the temporary to avoid node exporter seeing half a file. alarm(0);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Multiprocess - child process cannot be finished successfully
by bliako (Abbot) on Sep 09, 2022 at 21:50 UTC | |
It really depends on what kind of processing you are doing in the child and whether $filename should be renamed only outside the loop, because that's what you just did when you added that missing curly bracket. Is each child writing to the $filename cumulatively and only when all children are finished writing to it you want to rename it? To me it does not sound right. But I don't know what you are trying to do and your code makes it difficult to guess. And the alarm call, setting the alarm(60) and then canceling it (alarm(0), inside or outside the loop?) makes it even more difficult ... Now that you have the fork semantics working, try and isolate what you want to go inside the alarm in a new function and ask yourself when should files be closed, alarms cancelled and files renamed. Inside or outside the loop it may not matter for Perl, it may not be a syntax error but you may end up blowing up your cpu :) So, step back and think the problem on paper without writing code for a while. | [reply] [d/l] [select] |
by wonderG (Novice) on Sep 10, 2022 at 02:04 UTC | |
| [reply] [d/l] |
by Marshall (Canon) on Sep 11, 2022 at 07:51 UTC | |
Your code here looks backwards. You need the alarm because presumably the network setup is a blocking operation? You need to set up the alarm and turn it on BEFORE you enter into code that potentially will "freeze". I would suggest you make a safe_print() like I did to cut down on the "noise". I don't understand what these targets are? my $array = $targets->[$index]{label}; This simple test runs on Win10. Should be ok on Unix also. Update: Added a time stamp to original code. preserved in readmore tags.
Read more... (3 kB)
The code that you are using to test the network connection would be highly relevant here. There are timeout values for the typical socket connection request, etc. You may not need to use an ALRM. Also, I don't see much need to write to the log file in case of flock(),fork() failure, perhaps you will do just as well with "die" for those super massive errors? Something is super wrong if the O/S cannot fork! | [reply] [d/l] [select] |
by wonderG (Novice) on Sep 10, 2022 at 08:08 UTC | |
| [reply] [d/l] |