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 definedWhat I am doing wrong?
-
@gregcope you call _thread.start_new_thread(feedMicroGPS, '') can't find
feedMicroGPSwhich 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.
-
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.
-
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.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login