1

I am writing a python program that is supposed to make a GPS measurement every second while at the same time conducting some other measurements. I eventually tend to do this using two threads, and now I'm trying to write the GPS-thread.

I wrote a version of it according to the method make measurement - wait - reiterate:

navigator = GPS()
data = []

def avg(xlist):
    a=sum(xlist)/len(xlist)
    return a
x=[]
y=[]
for i in range(20):
    data.append(navigator.GetData())
    x.append(data[i]['x'])
    y.append(data[i]['y'])
    print "x: ",x[i],", y: ",y[i]
    time.sleep(1)
navigator.CloseThread()
del navigator

xavg=avg(x)
yavg=avg(y)

I suppose if I write the program in this manner it will 1. Be a CPU hog 2. Not make measurements at even seconds. What should I do?

user2536262
  • 279
  • 2
  • 8
  • 22
  • 2
    https://docs.python.org/2/library/threading.html#timer-objects You could possible use threading.Timer – M4rtini May 12 '14 at 12:51
  • This works excellent when I want to wait for something once, but if I try to place it in a loop or stop it and start it again it crashes. – user2536262 May 12 '14 at 14:12
  • 1. Will it make measurements at odd seconds? (even, odd) 2. What you should do depends on what you need. I think your program works great. 3. What is a CPU hog? What is making it a CPU hog? Could you go more into detail with this? Maybe it is just a fear and nothing real. – User May 12 '14 at 14:41
  • 1. Whole seconds. What I meant was that the program as I've written it will wait one second and then make the measurement, making the entire cycle slightly longer than a second. 2. I fear time.sleep() will halt the entire program so that nothing else can happen while it waits. – user2536262 May 13 '14 at 07:12
  • Note that the Global Interpreter Lock (GIL) applies to `threading.Thread`. – moooeeeep Jun 10 '14 at 12:30

0 Answers0