3

I don't know if this is a simple question or impossible or anything, but I couldn't find anything on it so I figured I would ask it.

Is it possible to return values from a while loop while that loop is still running? Basically what I want to do is have a vector constantly updating within a while loop, but able to return values when asked without stopping the while loop. Is this possible? Do I just have to break up the program and put the while loop in a separate thread, or can I do it within one function?

Also I would prefer a method that is not computationally intensive (obviously), and one compatible with a rate-limited while loop as this one will certainly be rate-limited.

Again, if this is a stupid question just tell me and I will delete it, but I wasn't able to find documentation on this.

Code I am trying to implement this with:

def update(self, x_motion, y_motion, z_motion):
        self.x_pos += self.x_veloc
        self.y_pos += self.y_veloc
        self.z_pos += self.z_veloc
        self.x_veloc += self.x_accel
        self.y_veloc += self.y_accel
        self.z_veloc += self.z_accel
        self.x_accel = x_motion[2]
        self.y_accel = y_motion[2]
        self.z_accel = z_motion[2]
while True:
self.update(x_motion, y_motion, z_motion)

print vector.x_accel

Something along those lines at least. It is important that these return outside of the while loop, so that the while loop runs in the background, but it only gives results when asked, or something like that.

Slater Victoroff
  • 21,376
  • 21
  • 85
  • 144
  • 2
    You need to give more specifics about what you're trying to do, ideally some example code. It sounds like generators may help you out, but it's hard to tell from your description. – BrenBarn Jun 23 '12 at 19:51
  • Does the while loop need to keep running operations concurrently? A generator can pause and return a value and then resume where it left off when the next value is requested. But it does pause between values. – jdi Jun 23 '12 at 19:54
  • It could pause, and I was thinking of doing that, but I would really rather not pause the while loop if at all possible. – Slater Victoroff Jun 23 '12 at 20:01

3 Answers3

6

Create a generator instead.

def triangle():
  res = 0
  inc = 1
  while True:
    res += inc
    inc += 1
    yield res

t = triangle()
print next(t)
print next(t)
print next(t)
print next(t)

EDIT:

Or perhaps a coroutine.

def summer():
  res = 0
  inc = 0
  while True:
    res += inc
    inc = (yield res)

s = summer()
print s.send(None)
print s.send(3)
print s.send(5)
print s.send(2)
print s.send(4)
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • I was about to answer with a generator as well, but I was confused about the OPs intentions. He said the while loop should keep running, so I assume there must be other actions going in with the functions. Part of it sounds like a thread or just a no_wait queue. – jdi Jun 23 '12 at 19:56
  • jdi is right actually, I would need something that doesn't just return everything being spit out by the while loop. Is there a way of implementing that with yield? – Slater Victoroff Jun 23 '12 at 20:01
4

What you are looking for is yield:

def func():
    while True:
        yield "hello"


for x in func():
    print(x)

Generators can also be written like list comprehensions:

Have a look at this question:

What does the "yield" keyword do?

W. Churchill
  • 346
  • 1
  • 7
  • 28
beoliver
  • 5,579
  • 5
  • 36
  • 72
  • Would this work outside of the while loop? It doesn't really help me to keep it inside. – Slater Victoroff Jun 23 '12 at 19:59
  • do you know exactly what you want to return? how often, and in what format? – beoliver Jun 23 '12 at 20:03
  • I want to return a tuple, at variable time spans, something between half a second and a second, with the updates happening at irregular intervals. Format is immaterial. – Slater Victoroff Jun 23 '12 at 20:13
  • what you can do is add functions / methods into the loop, that can do things with your tuple – beoliver Jun 23 '12 at 20:29
  • You are fundamentally misunderstanding my problem. I want to do things outside of the loops so that these two processes need not be linked. I am dealing with two asynchronous processes, I will probably use threads. – Slater Victoroff Jun 23 '12 at 20:32
0

If I understand you and the code you want to do.

I may think of trying to find equations of position, velocity and acceleration with respect to time.

So you won't need to keep, the while loop running, what you need is to convert it to a function, and using the time difference between the 2 function calls, you can perform the calculations you want, only when you call the function, rather than having the while loop running all the time.

Mahmoud Aladdin
  • 536
  • 1
  • 3
  • 13
  • I'm not dealing with a function, I'm dealing with input from an irregular source, as all useful programs do. – Slater Victoroff Jun 25 '12 at 22:03
  • Well, given the fact that you want an action to break the while loop, take the values it has at that moment of time, and let it go on with its work as it is. What I was referring to, that instead of the while loop running, you can calculate that values you want from the loop, only at the moments, where you want this values. – Mahmoud Aladdin Jun 26 '12 at 20:34