Example #1
0
    def _agent_accepting_travelers(self, name) -> None:
        agent = Resource(1)  # Models how the agent is busy processing a traveler.
        while True:
            # Are we on break yet? That coffee won't drink itself.
            self._agents_working.wait()
            info(f"Agent {name}/{self.num} ready")

            # Is anybody in there?
            if self._travelers_waiting.is_empty():
                debug(f"Agent {name}/{self.num} waiting for travelers")
                self._traveler_ready.turn_off().wait()
                continue  # Check back if we've gone on break while waiting for somebody.

            # Accept the next traveler traversing the checkpoint.
            traveler_next = self._travelers_waiting.peek()
            debug(f"Agent {name}/{self.num} about to process traveler {traveler_next.local.name}")
            traveler_next.local.agent = agent
            traveler_next.local.agent_name = f"{name}/{self.num}"
            self._travelers_waiting.pop()

            # Allow the next traveler to "use" this agent, so we may then wait until it's done traversing.
            advance(0.0)
            debug(f"Agent {name}/{self.num} doing the processing.")
            with agent.using():
                debug(f"Agent {name}/{self.num} done with the processing.")
Example #2
0
def take_many(resource: Resource, delay: float, log: List[float]) -> None:
    with resource.using(int(delay)):
        do_while_holding_resource(delay, log)