add multiproc capability
authorgc <gc>
Sat, 5 Mar 2005 14:21:35 +0000 (14:21 +0000)
committergc <gc>
Sat, 5 Mar 2005 14:21:35 +0000 (14:21 +0000)
booh

diff --git a/booh b/booh
index f946f2c6cdfe8e99893edc4310252bed515ac91e..f5ba7962a06fe1fa93d4b3781d5b41f22791d5cc 100755 (executable)
--- a/booh
+++ b/booh
@@ -34,6 +34,8 @@ $options = [
                          
     [ '--theme',         '-t', GetoptLong::REQUIRED_ARGUMENT, "Select HTML theme to use" ],
 
+    [ '--mproc',         '-m', GetoptLong::REQUIRED_ARGUMENT, "Specify the number of processors for multi-processors machines" ],
+
     [ '--verbose-level', '-v', GetoptLong::REQUIRED_ARGUMENT, "Set max verbosity level (0: warnings, 1: important messages, " +
                                                               "2: other messages)" ],
 ]
@@ -94,6 +96,10 @@ def handle_options
             when '--theme'
                 $theme = arg
 
+            when '--mproc'
+                $mproc = arg.to_i
+                $pids = Array.new()
+
             when '--verbose-level'
                 $verbose_level = arg.to_i
 
@@ -143,9 +149,25 @@ def build_html_skeleton
     return theme_file
 end
 
-def sys(cmd)
-    msg 1, cmd
-    system(cmd)
+#- parallelizable sys
+def psys(cmd)
+    if $mproc
+        if pid = fork
+            $pids << pid
+        else
+            msg 1, cmd + ' &'
+            system(cmd)
+            exit 0
+        end
+        if $pids.length == $mproc
+            finished = Process.wait2
+            $pids.delete(finished[0])
+            $pids = $pids.find_all { |pid| Process.waitpid(pid, Process::WNOHANG) == nil }
+        end
+    else
+        msg 1, cmd
+        system(cmd)
+    end
 end
 
 def walk_source_dir
@@ -175,7 +197,7 @@ def walk_source_dir
                 if orientation =~ /left - bottom/
                     convert_options += '-rotate -90 '
                 end
-                sys "#{$convert} #{convert_options}-geometry 704x528 '#{img}' '#{dest_img}'"
+                psys("#{$convert} #{convert_options}-geometry 704x528 '#{img}' '#{dest_img}'")
             end
         }