Run python script continuos

Hello, I am very new to using this python in blender. I am planning to do a flocking behavior using python in blender. As a part of this I have stuck on how to run the python script continuously. For example, I want to move an object in -y direction

import bpy
import mathutils


cube = bpy.data.objects["Cube"]
vect = mathutils.Vector(0.0, -0.3, 0.0)

cube.location = cube.location + vect

and when I run the script, the object moved once and stopped but i want to make it move as long as the session of blender is going. Can anyone help me with this?

Thank You

A modal operator might be a better option for a task like this. Modal operators will be run every time a new “event” is detected in their main run loop (like the mouse cursor getting moved or a key being press) until they return {“FINISHED”} or {“CANCELLED”}. I have an example script here that can be run from the internal script editor and accessed through the space bar menu (type in the bl_label).

Another option might be scripting something using the Game Engine.

Thanks for the reply, Is there an event related to play button (bpy.ops.screen.animation_play()) on timeline in the blender which means if the play button is clicked I want a cube to move randomly in the user perspective window and when the stop button is clicked the object movement should stop.

Game engine is not an option for me

If you want it to be animated, the best way is to use drivers. You can register functions in the driver namespace and then call them from drivers (although it isn’t necessary for something simple like moving on Y).