Making the game engine automatically run.

Hi all, I’m fairly new to Blender scripting and at the moment I am kind of at a loss of where to start for this. What I want is for when I press P to run the game engine it will automatically start and then stop again after a set amount of time until I press P again. I’m doing this for creating a simulation of Buffon’s Needle so any and all help would be appreciated.