Running python functions timed

So im using a python library for using a lidar-sensor

Im trying to get it somehow to start the initialisation of the lib-stuff at the beginning and than at an fixed intervall read the values from and start again.

I dont fully understand how this run command works, manual says, its used to run a whole text dat, but i saw also running a string as a first argument.
my Code gives me an “name ‘initSensor’ is not defined” exception.
its a bit strange, i think it works 1-2 times, to run that function from itself and after that it throws that error…

What am i doing wrong? or some general tipps how to approach this?


import PyLidar3
import time

import threading

def initSensor():
    port = "COM7"
    Obj = PyLidar3.YdLidarX4(port)
    isConnected = Obj.Connect()

        gen = Obj.StartScanning()
        return gen
        print("connection failed, try to reconnect Sensor")
        #start_time = threading.Timer(1, initSensor())
        init_print = "initSensor()"
        #return null

def getData(gen):
        if op("scanningSwitch")["chan1"] == 1:
            print("getting data")
            data = next(gen)
            for key in data:

            run("getData(gen)", delayFrames=60)

gen = initSensor()

I solved it with passing an additional argument “fromOP = me” to the run function