We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,259 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Perl separate process

Hi all. I have a process that checks the last time a file was updated. If the data is over 3 minutes a separate perl script is started. The problem is that second script takes 5-10 seconds to run. Is there a way to start the second script, but continue and finish the current script?

..... CODE .............

if ($time_difference > 180) {
    system("perl create_cache.pl");
}

...... MORE CODE .......

Thanks,

3
Contributors
3
Replies
2 Weeks
Discussion Span
4 Months Ago
Last Updated
6
Views
neoanomally
Newbie Poster
13 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Since I just answered another question similar to this, I'd suggest using fork() depending on if it works on your OS or not (works fine on Linux/Unix and for me just now on Win7)

Change:

if ($time_difference > 180) {
system("perl create_cache.pl");
}

to:

if ($time_difference > 180) {
my $pid=fork();
if (undef $pid){die "fork failed: $!\n";}
if (!pid)
  {
  # May want to close any open sockets or filehandles
  # here in the child, before running the system 
  # command to avoid them hanging if the parent 
  # closes them before the system call completes in 
  # the child process.
  system("perl create_cache.pl");
  exit; # child exits
  }
else
  {
  # parent continues on...
  }
}
kordaff
Newbie Poster
23 posts since Jun 2005
Reputation Points: 11
Solved Threads: 0
Skill Endorsements: 0

Thank you very much. I've been looking specifically how to use fork(). I read through some documentation but wasn't sure how to actually use it.

In line 4, what is the if statement checking exactly? Because you are setting the $pid =fork()?

Thanks!

neoanomally
Newbie Poster
13 posts since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

The $pid is the process ID created by the fork() if it is successfully created. The if(!pid) should be if($pid). I think it is just a typo. You should always use strict when you write Perl script.

Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.1098 seconds using 2.68MB