First, this appears to be in a tag change event. You should avoid long running scripts in tag change events. I would recommend that you refactor this to be used with a Gateway Tag Change Event.
This is due to the way that Tag Change Events work. You can read more on that here:
Second, you should be using system.runPrepUpdate()
from itertools import chain
if not initialChange:
logstart = system.date.now()
logger = system.util.getLogger("Heartbeat event trigger")
oktoreadpath = '[default]_tmp/_HeartBeat_event_monitoring/ReadAreaOk2Read'
ReadLengthpath = '[default]_tmp/_HeartBeat_event_monitoring/ReadLength'
dbresultpath = '[default]_tmp/_HeartBeat_event_monitoring/DBResult'
paths = [oktoreadpath,ReadLengthpath]
[oktosend , readlength] = system.tag.readBlocking(paths)
readlength = readlength.value
if oktosend.value == 1 and readlength >0:
parentPath = '[default]_tmp/_HeartBeat_event_monitoring/_HeartBeat_event_read/_DPP_Read_HBEvent_{}_/'
eventpaths = [pPath + name for pPath in [parentPath.format(e) for e in range(readlength)] for name in ['Integer00','Integer01','Integer02','Integer03','TimeInBuffer']]
taglist = system.tag.readBlocking(eventpaths)
querystring = "INSERT INTO heartbeat_event (equipid,heartbeatc,heartbeatp,tsc,createdate,logid) values "
querystring += ','.join(['(?,?,?,?,?,?)'] * (readlength / 5))
querystring += ';'
def step(xs):
start = 0
step = 5
for x in xs:
if start > (len(xs)-step):
break
yield (start, xs[start])
start += step
eventlist = list(chain.from_iterable([[taglist[y].value,taglist[y+1].value,taglist[y+2].value,system.date.addMillis(currentValue.timestamp,taglist[y+4].value*(-1),currentValue.timestamp, taglist[y+3].value] for y,tag in step(taglist)]))
system.db.runPrepUpdate(querystring,eventlist,_dataBase)
logstop = system.date.now()
logger.info("heart beat trigger end successed take {}ms".format(system.date.millisBetween(logstart,logstop))