Ticker deadlocks sometimes

24
July 16, 2018, at 00:30 AM

My Ticker class is intended for a master thread to increment the ticker every time a set of registered slave threads have completed one iteration of their jobs. This seems to work fine but once in a while I end up with a deadlock. The last time I ran, it deadlocked on the 451st time. I am not able to see what is causing the deadlock. I am using python3.6

#ticker.py
import threading
class Ticker:
    def __init__ (self):
        self.currentTick = 0
        self.functionStatus = {}
        self.uptick = threading.Event()
    def registerFunction (self, funcId):
        self.functionStatus[funcId] = threading.Event()
    # This has to be called only by one master thread
    def createNextTick(self):
        for k, cond in self.functionStatus.items():
            # wake up when each slave thread has finished it share of work
            cond.wait()
        for k, cond in self.functionStatus.items():
            cond.clear()
        self.currentTick += 1
        self.uptick.set()
        self.uptick.clear()
        return self.currentTick
    def isDone (self, funcId):
        self.functionStatus[funcId].set()
    def waitForUptick (self):
        self.uptick.wait()
    def getCurrentTick (self):
        return self.currentTick
    def complete(self):
        for k, cond in self.functionStatus.items():
            cond.set()
        self.uptick.set()

# ticker_test.py
from ticker import *

def laneAccl(numSecs):
    global laneRunning
    funcId = "laneAccl_"+ str (threading.get_ident())
    globTicker.registerFunction(funcId)
    while laneRunning:
        globTicker.isDone (funcId)
        globTicker.waitForUptick()
def sigChange(numSecs):
    global sigRunning
    color = "red"
    funcId = "sigChange_"+ str (threading.get_ident())
    globTicker.registerFunction(funcId)
    while sigRunning:
        for i in range (numSecs):
            globTicker.isDone (funcId)
            globTicker.waitForUptick()
        if color == "red":
            color = "green"
        elif color == "green":
            color = "orange"
        else:
            color = "red"
def startFunctionThreads():
    tArr = []
    for i in range (50):
        for tup in [(laneAccl, 1), (sigChange, 5)]:
            t = threading.Thread (target=tup[0], args=(tup[1], ))
            t.start()
            tArr.append (t)
    return tArr
import time

if __name__ == "__main__":
    laneRunning = True
    sigRunning = True
    globTicker = Ticker()
    tArr = startFunctionThreads ()
    numTicks = 500
    beg = time.time()
    while globTicker.createNextTick() < numTicks:
        pass
    print ("Total time:", time.time() - beg)
    laneRunning = False
    sigRunning = False
    globTicker.complete()
    for t in tArr:
        t.join()
READ ALSO
What is the difference between the terms accuracy and validation accuracy

What is the difference between the terms accuracy and validation accuracy

I have used LSTM from Keras to build a model that can detect if two questions on Stack overflow are duplicate or notWhen I run the model I see something like this in the epochs

24
How to add hours to specific timestamps in a pandas df

How to add hours to specific timestamps in a pandas df

I have a pandas df that contains a Column of timestampsSome of the timestamps are after midnight

35
Problems Creating CSV from Python with Array of Arrays

Problems Creating CSV from Python with Array of Arrays

So I have a python that collects data points from various DAQ units and all I want to do is spit them out into a CSVI had the code working perfectly with my sample arrays, but I can't get this working for the life of me now

44
Can I pip install python3.6?

Can I pip install python3.6?

If I have an older version of python (eg

47