Esempio n. 1
0
    def __init__(self, initial_time=0, factor=1.0, strict=True):
        Environment.__init__(self, initial_time)

        self.env_start = initial_time
        self.real_start = time()
        self.factor = factor
        """Scaling factor of the real-time."""
        self.strict = strict
        """Running mode of the environment. :meth:`step()` will raise a
Esempio n. 2
0
    def step(self):
        """Process the next event after enough real-time has passed for the
        event to happen.

        The delay is scaled according to the real-time :attr:`factor`. With
        :attr:`strict` mode enabled, a :exc:`RuntimeError` will be raised, if
        the event is processed too slowly.

        """
        evt_time = self.peek()

        if evt_time is Infinity:
            raise EmptySchedule()

        real_time = self.real_start + (evt_time - self.env_start) * self.factor

        if self.strict and time() - real_time > self.factor:
            # Events scheduled for time *t* may take just up to *t+1*
            # for their computation, before an error is raised.
            raise RuntimeError('Simulation too slow for real time (%.3fs).' % (
                time() - real_time))

        # Sleep in a loop to fix inaccuracies of windows (see
        # http://stackoverflow.com/a/15967564 for details) and to ignore
        # interrupts.
        while True:
            delta = real_time - time()
            if delta <= 0:
                break
            sleep(delta)

        return Environment.step(self)