[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Edlug Archive Mar 2004 ]

Re: [edlug] Perl - Only run n instances of script?



Steve Kemp writes:
 > On Thu, Mar 11, 2004 at 10:32:50AM +0000, Lee Kindness wrote:
 > > Anyone got a quick way of limiting/locking a script so that it can
 > > only be run, for example, 4 times concurrently? Any other runs of the
 > > script would block until a "slot" was available.
 > 
 >   Touch a file in the BEGIN { } block, and then remove it in the DESTROY
 >  {} section.
 >   Then upon initialization count the number of files /var/run/foo.$$.
 >   That's a simple solution that should work fairly well, requires no
 >  IPC and is not perl specific.
 >   Perhaps if you explained what it is that you're trying to do you'd get
 >  a more specific answer.

Right, a script is executed to compress a large list of files and
update the status of these files in a database. The execution of this
script is triggered by an external event, and thus there can be any
number of them running. However, obviously to prevent the machine from
being too loaded and preserve db connections you need to limit the
number of these script actually compressing files. The rest can sit
idle until another has completed.

I've currently got a flock lock, so one 1 script is active and the
rest are blocking until it completes, at which time one of them
becomes active. However this does not make best use of resources on
the machine (4 CPUs), so it'd be best if more than 1 script were
running concurrently...

thanks, L.
-
----------------------------------------------------------------------
You can find the EdLUG mailing list FAQ list at:
http://www.edlug.org.uk/list_faq.html



This archive is kept by wibble@morpheux.org.DONTSPAMME
homepage