threading error (NameError: name 'xxx' is not defined)



  • hi all,

    I am trying to make a Class that merges L76GNSS and micropyGPS. I would like to create a thread to feed the micropyGPS object with NMEA data in the background.

    My code is here; https://github.com/gregcope/L76micropyGPS.

    Basically;

    class L76micropyGPS:
    
        def __init__(self, my_gps, pytrack=None, sda='P22', scl='P21', timeout=None):
        <snippage>
        # start thread feeding microGPS
            _thread.start_new_thread(feedMicroGPS, '')
    
        def feedMicroGPS(self):
           # <snippage>
            while True:
               
                # get some NMEA data
                someNmeaData = self.i2c.readfrom(GPS_I2CADDR, 128)
                print(" feedGps_thread - gpsChars recieved : {}".format(len(someNmeaData)))
    
                # Pass NMEA data to micropyGPS object
                for x in someNmeaData:
                    self.my_gps.update(x)
    

    However I get this error;

    Traceback (most recent call last):
    File "main.py", line 21, in <module>
    File "/flash/lib/L76micropyGPS.py", line 41, in init
    NameError: name 'feedMicroGPS' is not defined

    What I am doing wrong?



  • Functions and methods are actually different things and exist in different scopes.
    You can't look up a method by name without the self. (or similiar) prefix. Unless you bind it to another name in the current scope.

    The same applies to class methods.



  • Brilliant. Thanks. Now fails much further on!

    I had assumed that as methods in python are declared as functions that this should just work(tm).

    Thanks again.



  • @gregcope you call _thread.start_new_thread(feedMicroGPS, '') can't find feedMicroGPS which is to be expected, it's definition is not in the scope of the init method.

    You should either be using self.feedMicroGPS or cls.feedMicroGPS (if it where a class method).

    The examples use a function not a method.


Log in to reply
 

Pycom on Twitter