Now can launch one process per core, and launch new ones when the old ones end

This commit is contained in:
Sergio Costas 2014-07-23 23:53:11 +02:00
parent 13f9e67eb1
commit 86f77faa3f
13 changed files with 761 additions and 77 deletions

132
src/devede/runner.py Normal file
View file

@ -0,0 +1,132 @@
# Copyright 2014 (C) Raster Software Vigo (Sergio Costas)
#
# This file is part of DeVeDe-NG
#
# DeVeDe-NG is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# DeVeDe-NG is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
from gi.repository import Gtk,GObject
import os
import devede.configuration_data
class runner(GObject.GObject):
def __init__(self):
self.config = devede.configuration_data.configuration.get_config()
if (self.config.multicore):
self.count_cores()
else:
self.cores = 1
self.proc_list = []
self.running = 0
self.builder = Gtk.Builder()
self.builder.set_translation_domain(self.config.gettext_domain)
self.builder.add_from_file(os.path.join(self.config.glade,"wprogress.ui"))
self.builder.connect_signals(self)
self.wprogress = self.builder.get_object("progress")
self.wprogress.show_all()
self.wtotal = self.builder.get_object("progress_total")
progress_frame = self.builder.get_object("progress_frame")
box = Gtk.Box(Gtk.Orientation.VERTICAL, 0)
progress_frame.add(box)
box.show()
self.progress_bars = []
self.used_progress_bars = []
for c in range(0,self.cores):
f = Gtk.Frame()
p = Gtk.ProgressBar()
p.set_orientation(Gtk.Orientation.HORIZONTAL)
f.add(p)
# A frame, a progress bar, and the process running in that bar
self.progress_bars.append([f, p, None])
box.pack_start(f,True,True,0)
box.set_orientation(Gtk.Orientation.VERTICAL)
self.total = 0
def add_processes(self,processes):
for p in processes:
p.append(None) # this will contain the progress bar being used by this process
self.proc_list.append(p)
self.total_processes = len(self.proc_list)
def on_cancel_clicked(self,b):
pass
def count_cores(self):
self.cores = 0
proc_file = open("/proc/cpuinfo","r")
for line in proc_file:
if (line.startswith("processor")):
self.cores += 1
def run(self):
for element in self.proc_list:
# each element has three items:
# * the process object
# * the list of dependencies, or None if there are no more dependencies
# * the progress bar being used by this process
if (element[1] == None) and (element[2] == None):
self.progress_bars[0][2] = element[0]
element[0].connect("ended",self.process_ended)
element[0].run(self.progress_bars[0])
element[2] = self.progress_bars[0]
self.used_progress_bars.append(self.progress_bars[0])
if (len(self.progress_bars) > 1):
self.progress_bars = self.progress_bars[1:]
else:
self.progress_bars = []
break
self.wtotal.set_text(str(len(self.proc_list)-self.total)+"/"+str(self.total))
def process_ended(self,process, retval):
# move the progress bar used by this process to the list of available progress bars
tmp = []
for e in self.used_progress_bars:
if (e[2] == process):
e[2] = None
self.progress_bars.append(e)
e[0].hide()
else:
tmp.append(e)
self.used_progress_bars = tmp
# remove this process from the list of processes, and remove it from the dependencies in other processes
tmp = []
for e in self.proc_list:
if (e[0] != process):
tmp.append(e)
if (e[1] != None):
tmp2 = []
for dep in e[1]:
if dep != process:
tmp2.append(dep)
if (len(tmp2) != 0):
e[1] = tmp2
else:
e[1] = None
self.proc_list = tmp
# launch a new process
if (len(self.proc_list) != 0):
self.run()
else:
self.wprogress.destroy()