
| Current Path : /proc/thread-self/root/home/ift/52_procpy/dataninja/ |
Linux ift1.ift-informatik.de 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64 |
| Current File : //proc/thread-self/root/home/ift/52_procpy/dataninja/prcmanager.py |
# -*- coding: utf-8 -*-
import os
import re
import sys
import time
import procpy
import random
import json
#*************************************************************************
class Prcmanager (object):
def __init__ (self,db):
self.db = db
#*************************************************************************
def projection_process (self,entry,params=[]):
"""
Takes a python object (entry, maybe queried formerly by query_data) and
executes the 'next' function which is stored in the entry.JUMP variable
If no new entry.JUMP is set in that function, it will be computed
as the 'next' function in the object class
If entry is not an object, it can be taken as an object class,
and entry is initialised as an instance of that object class.
After the execution of the function the entry will be dumped into the
database table entries .
Additionally, if entry.OBJID does not (yet) exist, a value for
that will be computed, so that the uniqueness constraint for the column
OBJID will be fulfilled. If entry has a method next_test (which
returns the decessor pattern for a given pattern), this method will
be used, otherwise labels from 001, 002, 003, ... will be created
"""
msg = "proceeded"
try:
entry = re.sub(r"^(.*)\.py$", "\\1.Test", re.sub(r"[\\\/]", ".", entry, 9999))
m = re.search(r"^(.*?)\.?([^\.]*?)$",entry)
except:
m = None
if m: # new running item
exec('import ' + m.group(1))
try:
exec('entry = ' + entry + '(params)\n')
msg = "created"
entry.__func__ = ""
except Exception as e:
# print str(e)
return("")
if not m: # proceeding an item
if 'WAKETIME' in vars(entry):
if entry.WAKETIME < 0:
print("Entry is already running or waiting for child processes.")
print(entry.WAKETIME)
return("")
time_to_wait = entry.WAKETIME - time.time()
if time_to_wait > 0:
if 'JUMP' in vars(entry) and re.search(r"^\-",entry.JUMP):
del entry.JUMP
try:
entry.JUMP
print("Awake time not yet reached. " + str(int(time_to_wait)) + " sec to wait.")
return("")
except:
print("Process already ended...")
return("")
try:
entry.__func__ = entry.JUMP
del entry.JUMP
if not entry.__func__== "":
locked = self.db.lock_waketime(entry.MD5KEY,os.getpid())
if locked:
entry.__db__ = self.db
exec("entry." + entry.__func__ + "()")
del entry.__db__
except Exception as e:
entry.JUMP = entry.__func__
print(str(e))
entry.ERROR = str(e)
if not 'JUMP' in vars(entry):
entry.JUMP = ""
if not 'JUMP' in vars(entry): # computing the next jump function if entry.JUMP does not exist
entry.JUMP = entry.__func__
for k in dir(entry):
if re.search(r"^(run|r\d+|t\d+)$",k):
if entry.JUMP == "":
entry.JUMP = k
break
elif entry.JUMP == k:
entry.JUMP = ""
# Zusaetzliche Semantik:
#------------ 1. Zufallsauswahl bei ODER-Sprung
if type(entry.JUMP).__name__ == 'list':
entry.JUMP = entry.JUMP[0]
# jumps = {}
# for j in entry.JUMP:
# m = re.search(r"^(.*)\/(.*)$",j)
# if m:
# jumps[m.group(1)] = int(m.group(2))
# else:
# jumps[j] = [1]
# entry.JUMP = jumps
if type(entry.JUMP).__name__ == 'dict':
gesamt = 0
for j in entry.JUMP:
try:
gesamt = gesamt + float(entry.JUMP[j][0])
except:
gesamt = gesamt + float(entry.JUMP[j])
random_pointer = random.uniform(0,gesamt)
gesamt = 0
for j in entry.JUMP:
try:
gesamt = gesamt + float(entry.JUMP[j][0])
except:
gesamt = gesamt + float(entry.JUMP[j])
if gesamt >= random_pointer:
entry.JUMP = j
break
entry.PATH = entry.JUMP
elif 'PATH' in vars(entry):
del entry.PATH
else:
entry.PATH = entry.JUMP
#------------ 2. Prozessende
####if not bool(entry.JUMP):
#### entry.JUMP = ""
if entry.JUMP == "" or re.search(r"^\-",entry.JUMP) or entry.JUMP == {}:
#### if not bool(entry.JUMP) or re.search(r"^\-",entry.JUMP):
msg = msg + " <--------------- end of process reached...."
entry.SLEEP = 9999999999
try:
self.db.lock_waketime(entry.PARENTID)
except Exception as e:
pass
# open("__finished__.txt",'w').write("finished\n")
#------------ 3. get aggregate values from the childs
for fktfield in vars(entry):
if fktfield == 'TIME':
aggr = 'AVG'
elif fktfield == 'EFFORT':
aggr = 'SUM'
else:
continue
break
try:
cursor = self.cursor(entry.OBJID,'PARENTID',aggr+"("+fktfield+")")
sumval = self.db.next_obj(cursor)
self.db.lock_waketime(entry.OBJID,-1)
except:
pass
#-----------------
for o in list(vars(entry).keys()):
if o in ['SUMVALUES','SLEEP','WAKETIME']:
continue
exec('newval = entry.' + o)
if o == 'PATH':
try:
sumval = entry.SUMVALUES[o] + "/" + newval
except:
sumval = newval
else:
try:
newval = float(newval)
except:
continue
try:
sumval = entry.SUMVALUES[o]
except:
sumval = 0
try:
sumval = float(sumval)
except:
continue
sumval = str(sumval + newval)
try:
entry.SUMVALUES
except:
entry.SUMVALUES = {}
entry.SUMVALUES[o] = sumval
# delfields = ""
# for sumfield in vars(entry): # sum up numeric fields
# continue
# if sumfield[0:3] == "SUM":
# field = sumfield[3:]
# if field in vars(entry):
# exec 'sumval = entry.' + sumfield
# exec 'newval = entry.' + field
# try:
# sumval = str(float(sumval) + float(newval))
# except:
# if sumval == "." or sumval == "run":
# sumval = newval
# else:
# sumval = sumval + "/" + newval
# exec 'entry.' + sumfield + ' = sumval'
# if not field == 'PATH':
# continue
# delfields = delfields + 'del entry.' + field + "\n"
## print delfields
# exec delfields
if not 'SLEEP' in vars(entry):
entry.SLEEP = 9999999999
entry.WAKETIME = time.time() + float(entry.SLEEP)
entry.WAKETIME = str(min(entry.WAKETIME,9999999999))
fork_counter = 0
if 'FORK' in vars(entry): # Forking: Anlegen von Child-Prozessen
child_processes = []
entryfork = entry.FORK
entryjump = entry.JUMP
entryid = entry.MD5KEY
entryobj = entry.OBJID
del entry.MD5KEY
del entry.FORK
for childproc in entryfork.split(","):
entry.JUMP = childproc
entry.OBJID = entryobj
fork_counter = fork_counter - 1
self.write_process(entry)
print("Child-Process " + entry.OBJID + " generated ...")
del entry.PARENTID
entry.MD5KEY = entryid
entry.OBJID = entryobj
entry.WAKETIME = fork_counter
entry.SLEEP = 0
entry.JUMP = entryjump
entry.CHILDS = child_processes
self.write_process(entry)
#print entry.OBJID + " " + msg
if not 'TIME' in vars(entry):
entry.TIME = 2
if entry.__func__ == "":
entry.TIME = 0
return entry
#*************************************************************************
def run_process (self,object_identifier="",param=[]):
entry = self.read_process(object_identifier)
if object_identifier == '___dryrun___': # ___dryrun___
if not entry:
return(None)
if 'WAKETIME' in vars(entry) and entry.WAKETIME >= 0:
entry.WAKETIME = 10
if not entry:
entry = object_identifier
return( self.projection_process(entry,param) )
#*************************************************************************
def read_process (self,object_identifier):
cursor = self.cursor(object_identifier,"OBJID")
entry = self.next_obj(cursor)
return(entry)
#*************************************************************************
# dbh.cursor("sql-portion","","*")
def cursor (self,object_identifier="",datafield="",fields="*"):
cursor = self.db.query_data(object_identifier,datafield,fields)
return(cursor)
#*************************************************************************
def next_obj (self,cursor):
o = self.db.next_obj(cursor)
entry = procpy.objpickle.from_mongo_dict( o )
return(entry)
#*************************************************************************
def write_process (self,entry):
set_parent_id = True
while (0 == 0):
if 'OBJID' in vars(entry): # computing a unique OBJID if it does not exist
erg = self.db.upsert( procpy.objpickle.to_mongo_dict(entry) )
if erg:
self.db.commit()
return(entry.OBJID)
elif set_parent_id:
entry.PARENTID = entry.OBJID
set_parent_id = False
try:
m = re.search(r"^(.*)\.(.*)$",entry.OBJID)
objclass = m.group(1)
objnr = m.group(2)
except:
objclass = type(entry).__module__ + "." + type(entry).__name__
if '__prefix__' in vars(entry):
objclass = entry.__prefix__ + "." + objclass
objnr = ""
try:
objnr = entry.next_test(objnr)
except:
try:
objnr = ("%03u" % (int(objnr)+1))
except:
objnr = "001"
if objnr == "":
return(0)
entry.OBJID = objclass + "." + objnr