def sop(matA, matB, row, col): tutil.tlog("A={} B={}".format(row, col)) assert matA.shape[1] == matB.shape[ 0], "Matrix cannot be multiplied. A={}, B={}".format( matA.shape, matB.shape) # Sleep for a small random interval to see the threads in play tutil.sleepMilli(np.random.randint(10)) return np.sum(matA[row, :] * matB[:, col])
def producer(q, milli=None): for i in range(10): mesg = "Item #{:03d}".format(i + 1) q.put(mesg) tutil.tlog(mesg) tutil.sleepMilli(milli)
def main(): q = Queue(maxsize=3) tutil.tlog("Main started") threading.Thread(target=producer, name="tMaker", args=[q, 100]).start() threading.Thread(target=consumer, name="tTaker", args=[q, 300]).start() tutil.tlog("Main exited")
def consumer(q, milli=None): for i in range(10): mesg = q.get() tutil.tlog(mesg) tutil.sleepMilli(milli)
def do_something(zz, mesg=None): mesg = "" if mesg is None else " Mesg=" + mesg tutil.tlog("Sleeping for {} milli.{}".format(zz, mesg)) tutil.sleepMilli(zz) tutil.tlog("Done sleeping")