atcroft has asked for the wisdom of the Perl Monks concerning the following question:
I am looking for a way to limit the number of concurrent executions of a script to some value x, where [1 <= x <= n ]. Limiting to 1 instance appears to be the easy case, but it seems more difficult to limit it to just some n greater than 1.
Because of the manner in which the process may be launched (a filter launched by another process-and no, not email-related), it is conceivable that several thousand instances could be launched in the span of a few minutes; however, I wish to limit this to a much smaller (and processable) number (such as less than 100). Because of the time involved (and other potential issues), I am trying to avoid solutions such as the creation of a large number of lock files (due to speed issues), or ones susceptible to race-conditions (or the like).
One suggestion I encountered suggested the use of IPC::Semaphore as a way, by allowing processes to update a shared semaphore, and exit if the value it contained exceeded my limit. (I believe I understood that correctly-please correct me if I am wrong, however.) I seem to be having difficulties implementing this, although now it seems to be working for the moment. Does it look as if I have this correct, and can anyone suggest a better way, or any issues they may see with my implementation?
#!/usr/bin/perl -w use strict; use IPC::SysV qw(IPC_PRIVATE S_IRWXU IPC_CREAT); use IPC::Semaphore; srand; my $sem = new IPC::Semaphore( 1234, 1, S_IRWXU | IPC_CREAT ); my $semval = $sem->getval(0); $semval = 0 unless ( defined($semval) ); exit(0) if ( $semval >= 10 ); $sem->setval( 0, ( $semval + 1 ) ); # # Do work... or in this case, # print the value of $semval and sleep for a while # print $semval + 1, "\n"; sleep( int( rand(20) + 1 ) ); # # Decrement the semaphore value # $semval = $sem->getval(0); if ($semval) { $sem->setval( 0, ( $semval - 1 ) ); } else { $sem->remove; } __END__ # # Sample test execution (in bash): # # index=30 # while [ $index -gt 0 ] # do # perl test.pl & # sleep 1 # let "index=index-1" # done #
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Effective use of IPC::Semaphore to limit number of concurrent executions of a scxipt
by ptkdb (Monk) on Oct 16, 2003 at 01:22 UTC | |
by atcroft (Abbot) on Oct 16, 2003 at 10:04 UTC | |
by ptkdb (Monk) on Oct 16, 2003 at 12:23 UTC | |
by ptkdb (Monk) on Oct 16, 2003 at 15:03 UTC | |
|
•Re: Effective use of IPC::Semaphore to limit number of concurrent executions of a scxipt
by merlyn (Sage) on Oct 16, 2003 at 01:29 UTC | |
by ptkdb (Monk) on Oct 16, 2003 at 01:46 UTC |