Python - How can I force print statement before entering a never-ending loop?

95 views Asked by At

I was debugging some code that had some never-ending loop issues, and I sometimes couldn't figure out how to make informative print statements to help identify which loop was broken.

An example case is:

def fake_function():
    print 'I am about to enter a broken loop.'

    while True:
    #  Doesn't matter what I'm doing here.
        _ = None

When I then call fake_function, I don't get any output until I halt it.

A slightly larger example that might show why I wanted this looks like:

def another_fake_function():

    #  This loop is fine and not broken.  
    print 'I am entering a loop that might be broken.'    
    leave_loop = False
    while not leave_loop:
        #  Do a thing.
        leave_loop = True

    #  This loop actually is broken.
    print 'I am about to enter a different loop that might be broken.'
    while True:
        #  Doesn't matter what I'm doing here.
        _ = None

So, is there any way to really force python to print something immediately?

Thanks!

0

There are 0 answers