def installStatsLoop(statsFile, statsDelay):
  """Installs an interval loop that dumps stats to a file."""

  def dumpStats():
    """Actual stats dump function."""
    scales.dumpStatsTo(statsFile)
    reactor.callLater(statsDelay, dumpStats)

  def startStats():
    """Starts the stats dump in "statsDelay" seconds."""
    reactor.callLater(statsDelay, dumpStats)

  reactor.callWhenRunning(startStats)
Installs an interval loop that dumps stats to a file.