Break out of loop

I have a loop in the code that is supposed to keep executing a command until the user presses a button. The problem is, I cannot figure out what I should make the command of the button in order to break out of the loop. Here is a simplified version of the code I have right now:

import direct.directbase.DirectStart
from direct.gui.DirectGui import *
gbl = 0
def loop():
 global gbl
 while(gbl):
  print "loop go"

def toggle():
 if gbl: gbl = 0
 else: gbl = 1

b = DirectButton(text = ("toogle"), command = toggle, scale=.05)

Panda’s task system is by default single-threaded, which means that a loop like that can never be broken out of. Nothing else will run, not even the button that you have created, while that loop is running.

Instead of writing a tight loop, put whatever it is you are doing inside that loop in its own task.

David