Creating dynamic animation in blender using python scripts
If your function is just an infinite loop, you won't be able to do anything in Blender while this script is executing. It doesn't work this way.

For this purpose you can create a new modal operator instead. If you want it to run in the background checking for keyboard input events, you can add a timer event and use it with modal() function in {'PASS_THROUGH'} mode like here.

For some particular tasks it may be easier to use bpy.app.handlers instead. They take your function as argument and execute it on some specific event, e.g. current frame change. This code will execute my_very_complex_function() printing 'Executing algorithm...' in console every time the frame is changed:

import bpy

def my_very_complex_function(context, dummy): # must take 2 arguments
    print('Executing algorithm...')

bpy.app.handlers.frame_change_pre.append(my_very_complex_function)


This is just an example, you can put in way more complex functions