X-Git-Url: https://git.sthu.org/?a=blobdiff_plain;f=paralleljobs.py;h=6eb92dda40b803a9543ea161360170e4f50ead0a;hb=76576fca186993bcd55c521bd6018019fd0cc5d7;hp=c9e313d185b1fd5a7bccb2cd1a89bac53772faae;hpb=c0720bddfafeeba49eeb16524c15ab88fd960bc8;p=paralleljobs.git diff --git a/paralleljobs.py b/paralleljobs.py index c9e313d..6eb92dd 100755 --- a/paralleljobs.py +++ b/paralleljobs.py @@ -1,4 +1,12 @@ -#!/usr/bin/python +#!/usr/bin/env python +""" A simple tool to run jobs from a database in parallel.""" + +__author__ = "Stefan Huber" +__copyright__ = "Copyright 2013" + +__version__ = "1.0" +__license__ = "LGPL3" + import sys, getopt, os import sqlite3 @@ -25,7 +33,6 @@ def printStatusInfo(conn): c.close() - print(nototal, nodone, wldone, wltotal) perdone = 100.0*float(nodone)/float(nototal) perwl = 100.0*float(wldone)/float(wltotal) @@ -59,12 +66,13 @@ def runCmd(cmd): return exitcode, out, err def processJob(conn, jobid): - print("Process job %d" % (jobid)) c = conn.cursor() c.execute("SELECT cmd FROM jobs WHERE id=?", (jobid,)) cmd, = c.fetchone() + print("Process job %d: %s" % (jobid, cmd)) + ec, out, err = runCmd(cmd) c.execute("UPDATE jobs SET exitcode=?, done=1 WHERE id=?;", (ec, jobid)) @@ -159,6 +167,7 @@ COMMANDS: OPTIONS: -d database the database to process + -n num in -w mode, only perform num-many jobs -p cols-def create properties table with SQL column spec -v print output of the job's command @@ -199,9 +208,10 @@ if __name__ == "__main__": propdef = None work = False status = False + numjobs = None try: - opts, args = getopt.getopt(sys.argv[1:], "hd:c:p:wsv") + opts, args = getopt.getopt(sys.argv[1:], "hd:c:p:wsvn:") for opt, arg in opts: if opt == "-h": @@ -219,6 +229,8 @@ if __name__ == "__main__": status = True elif opt == "-v": verbose = True + elif opt == "-n": + numjobs = int(arg) else: print("Unknown option '", opt, "'.") @@ -247,12 +259,15 @@ if __name__ == "__main__": insertJobs(conn, cmds) if work: - while True: + n = 0 + while not numjobs or n < numjobs: + jobid = getNextJobId(conn) if jobid == None: print("All jobs have been started.") break processJob(conn, jobid) + n += 1 conn.close()