GUI unresponsive during long running scripts

How to keep the GUI responsive during a slow computation? (Simulation & animation generation in my case.)
I currently have a Cancel button but once the script goes into the simulation loop, I can’t click it anymore. Blender.redraw() doesn’t seem to work either.