def _parallel(plist, block = True): processes=[] for p in plist: if type(p)==list: for q in p: processes.append(q) else: processes.append(p) for p in processes: p.start() s = Scheduler() s.addBulk(processes) if block: s.join(processes)
def __init__(self, seconds, action=None): self.seconds = seconds self.posted = (None, None) self.s = Scheduler() self.p = None self.g = (self, action)
def test_scheduler_unschedule(): event = Event() scheduler = Scheduler() scheduler.schedule(event, 6) scheduler.schedule(Event(), 5) scheduler.schedule(Event(), 7) scheduler.unschedule(event) for priority, scheduled_event in scheduler.priority_queue._queue: assert scheduled_event is not event
def get_next_job(self, customer): '''Calls the correct scheduler, passing the customer and the list of queues in the system. The scheduler can be chosen by the customer by specifying in a config file.''' if(sum(customer.jobs) == 0): return -1 #return Scheduler.smallest_slowest_queue_next(customer, self.server, self.config) #return Scheduler.smallest_fastest_queue_next(customer, self.server, self.config) #return Scheduler.smallest_queue_next(customer, self.server) return Scheduler.naive(customer)
def get_next_job(self, customer): '''Calls the correct scheduler, passing the customer and the list of queues in the system. The scheduler can be chosen by the customer by specifying in a config file.''' if (sum(customer.jobs) == 0): return -1 #return Scheduler.smallest_slowest_queue_next(customer, self.server, self.config) #return Scheduler.smallest_fastest_queue_next(customer, self.server, self.config) #return Scheduler.smallest_queue_next(customer, self.server) return Scheduler.naive(customer)
class TestScheduler(unittest.TestCase): def setUp(self): self.s = Scheduler() def test_schedule(self): schedules = self.s.schedule([{ "department": "ART", "course_number": 210 }, { "department": "GEOL", "course_number": 102 }])
class TimeoutGuard(Guard): """ Timeout spawns a timer thread, when posted. If removed before timeout, then the timer thread is cancelled. """ def __init__(self, seconds, action=None): self.seconds = seconds self.posted = (None, None) self.s = Scheduler() self.p = None self.g = (self, action) # Timer expired, offer an active Channel Request def expire(self): op, req = self.posted if op == READ: ChannelReq(self.p, msg=None).offer(req) elif op == WRITE: req.offer(ChannelReq(self.p)) def post_read(self, reader): self.posted = (READ, reader) # Start process self.p = Process(self.expire) self.p.start() self.p.setstate(ACTIVE) # Put process on the scheduler timer queue self.s.timer_wait(self.p, self.seconds) def post_write(self, writer): self.posted = (WRITE, writer) # Start process self.p = Process(self.expire) self.p.start() self.p.setstate(ACTIVE) # Put process on the scheduler timer queue self.s.timer_wait(self.p, self.seconds) def remove_read(self, req): self.s.timer_cancel(self.p) def remove_write(self, req): self.s.timer_cancel(self.p)
def __init__(self, fn, *args, **kwargs): self.fn = fn self.args = args self.kwargs = kwargs # Create unique id self.id = str(random.random())+str(time.time()) # Greenlet specific self.greenlet = None # Synchronization specific self.state = None self.s = Scheduler() self.executed = False
type=str, default='../adv_agent/obs_rms.pkl') parser.add_argument('--x_method', type=str, default='grad') parser.add_argument('--surrogate_model', type=str, default="../agent-zoo/agent/YouShallNotPass_agent.pkl") parser.add_argument('--mimic_model_path', type=str, default="../agent-zoo/agent/mimic_model.h5") args = parser.parse_args() adv_agent_path = args.adv_agent_path adv_agent_norm_path = args.adv_agent_norm_path scheduler = Scheduler( annealer_dict={'lr': ConstantAnnealer(learning_rate)}) env_name = env_list[args.env] # define the env_path env_path = args.surrogate_model mimic_model_path = args.mimic_model_path env = gym.make(env_name) venv = SubprocVecEnv([ lambda: make_adv_multi2single_env(env_name, adv_agent_path, adv_agent_norm_path, False) for i in range(n_cpu) ]) venv = Monitor(venv, 0) rew_shape_venv = apply_reward_wrapper(single_env=venv,
def test_scheduler_tick_multiple_events(): executed_events = set() scheduler = Scheduler() scheduler.schedule(TrackedEvent("event one", executed_events), 1) scheduler.schedule(TrackedEvent("event two", executed_events), 2) scheduler.schedule(TrackedEvent("event three", executed_events), 2) scheduler.schedule(TrackedEvent("event four", executed_events), 3) scheduler.tick() assert len(scheduler.priority_queue._queue) == 3 assert executed_events == {"event one"} scheduler.tick() assert len(scheduler.priority_queue._queue) == 2 assert executed_events == {"event one", "event two"} scheduler.tick() assert len(scheduler.priority_queue._queue) == 1 assert executed_events == {"event one", "event two", "event three"} scheduler.tick() assert len(scheduler.priority_queue._queue) == 0 assert executed_events == { "event one", "event two", "event three", "event four" }
def test_scheduler_tick(): scheduler = Scheduler() scheduler.schedule(TrackedEvent("event one", set()), 1) latest_event = scheduler.tick() assert latest_event.id == "event one"
def setUp(self): self.pcb1 = PCB(3, 5, 10) self.pcb2 = PCB(2, 5, 10) self.pcb3 = PCB(1, 5, 10) self.scheduler = Scheduler()
last_checkpoint = step if step - log_interval > last_log: log_callback(logger, locals, globals) last_log = step return True model.learn(total_timesteps=total_timesteps, log_interval=1, callback=callback, seed=seed) if __name__ == "__main__": scheduler = Scheduler(annealer_dict={'lr': ConstantAnnealer(LR)}) # useless env_name = GAME_ENV # multi to single, apply normalization to victim agent's observation, reward, and diff reward. venv = SubprocVecEnv([ lambda: make_zoo_multi2single_env(env_name, VIC_AGT_ID, REW_SHAPE_PARAMS, scheduler, reverse=REVERSE, total_step=TRAINING_ITER) for i in range(N_GAME) ]) # test if REVERSE:
class TestScheduler(unittest.TestCase): # Arrange def setUp(self): self.pcb1 = PCB(3, 5, 10) self.pcb2 = PCB(2, 5, 10) self.pcb3 = PCB(1, 5, 10) self.scheduler = Scheduler() def test_whenISetTheSchedulerPolicyAsFifo_thenIThePCBsAsThePriorityStates(self): self.scheduler.set_as_fifo() self.scheduler.add(self.pcb1) self.scheduler.add(self.pcb2) self.scheduler.add(self.pcb3) self.assertEquals(self.scheduler.next(), self.pcb1) self.assertEquals(self.scheduler.next(), self.pcb2) self.assertEquals(self.scheduler.next(), self.pcb3) def test_whenISetTheSchedulerPolicyAsPriority_thenIThePCBsAsThePriorityStates(self): self.scheduler.set_as_pq() self.pcb1.set_priority(PCBPriorities().getPriorities().LOW) self.pcb2.set_priority(PCBPriorities().getPriorities().MEDIUM) self.pcb3.set_priority(PCBPriorities().getPriorities().HIGH) self.scheduler.add(self.pcb1) self.scheduler.add(self.pcb2) self.scheduler.add(self.pcb3) self.assertEquals(self.scheduler.next(), self.pcb3) self.assertEquals(self.scheduler.next(), self.pcb2) self.assertEquals(self.scheduler.next(), self.pcb1) def test_whenISetTheSchedulerPolicyAsRR_thenIThePCBsAsThePriorityStates(self): self.scheduler.set_as_rr(5) self.scheduler.add(self.pcb1) self.scheduler.add(self.pcb2) self.scheduler.add(self.pcb3) self.assertEquals(self.scheduler.next(), self.pcb1) self.assertEquals(self.scheduler.next(), self.pcb2) self.assertEquals(self.scheduler.next(), self.pcb3)
def setUp(self): self.s = Scheduler()
class Process(): """ Process(fn, *args, **kwargs) It is recommended to use the @process decorator, to create Process instances See process.__doc__ """ def __init__(self, fn, *args, **kwargs): self.fn = fn self.args = args self.kwargs = kwargs # Create unique id self.id = str(random.random())+str(time.time()) # Greenlet specific self.greenlet = None # Synchronization specific self.state = None self.s = Scheduler() self.executed = False def setstate(self, new_state): self.state = new_state # Reschedule, without putting this process on either the next[] or the blocking[] list. def wait(self): while self.state == ACTIVE: self.s.getNext().greenlet.switch() # Notify, by activating and setting state. def notify(self, new_state, force=False): self.state = new_state # Only activate, if we are activating someone other than ourselves # or we force an activation, which happens when an Io thread finishes, while # the calling process is still current process. if self.s.current != self or force: self.s.activate(self) # Init greenlet code # It must be called from the main thread. # Since we are only allowing that processes may be created in the main # thread or in other processes we can be certain that we are running in # the main thread. def start(self): self.greenlet = greenlet(self.run) # Main process execution def run(self): self.executed = False try: self.fn(*self.args, **self.kwargs) except ChannelPoisonException: # look for channels and channel ends self.__check_poison(self.args) self.__check_poison(self.kwargs.values()) except ChannelRetireException: # look for channel ends self.__check_retire(self.args) self.__check_retire(self.kwargs.values()) self.executed = True def __check_poison(self, args): for arg in args: try: if types.ListType == type(arg) or types.TupleType == type(arg): self.__check_poison(arg) elif types.DictType == type(arg): self.__check_poison(arg.keys()) self.__check_poison(arg.values()) elif type(arg.poison) == types.UnboundMethodType: arg.poison() except AttributeError: pass def __check_retire(self, args): for arg in args: try: if types.ListType == type(arg) or types.TupleType == type(arg): self.__check_retire(arg) elif types.DictType == type(arg): self.__check_retire(arg.keys()) self.__check_retire(arg.values()) elif type(arg.retire) == types.UnboundMethodType: # Ignore if try to retire an already retired channel end. try: arg.retire() except ChannelRetireException: pass except AttributeError: pass # syntactic sugar: Process() * 2 == [Process<1>,Process<2>] def __mul__(self, multiplier): return [self] + [Process(self.fn, *self.__mul_channel_ends(self.args), **self.__mul_channel_ends(self.kwargs)) for i in range(multiplier - 1)] # syntactic sugar: 2 * Process() == [Process<1>,Process<2>] def __rmul__(self, multiplier): return [self] + [Process(self.fn, *self.__mul_channel_ends(self.args), **self.__mul_channel_ends(self.kwargs)) for i in range(multiplier - 1)] # Copy lists and dictionaries def __mul_channel_ends(self, args): if types.ListType == type(args) or types.TupleType == type(args): R = [] for item in args: try: if type(item.isReader) == types.UnboundMethodType and item.isReader(): R.append(item.channel.reader()) elif type(item.isWriter) == types.UnboundMethodType and item.isWriter(): R.append(item.channel.writer()) except AttributeError: if item == types.ListType or item == types.DictType or item == types.TupleType: R.append(self.__mul_channel_ends(item)) else: R.append(item) if types.TupleType == type(args): return tuple(R) else: return R elif types.DictType == type(args): R = {} for key in args: try: if type(key.isReader) == types.UnboundMethodType and key.isReader(): R[key.channel.reader()] = args[key] elif type(key.isWriter) == types.UnboundMethodType and key.isWriter(): R[key.channel.writer()] = args[key] elif type(args[key].isReader) == types.UnboundMethodType and args[key].isReader(): R[key] = args[key].channel.reader() elif type(args[key].isWriter) == types.UnboundMethodType and args[key].isWriter(): R[key] = args[key].channel.writer() except AttributeError: if args[key] == types.ListType or args[key] == types.DictType or args[key] == types.TupleType: R[key] = self.__mul_channel_ends(args[key]) else: R[key] = args[key] return R return args
def test_scheduler_schedule(): scheduler = Scheduler() scheduler.schedule(Event(), 5) assert len(scheduler.priority_queue._queue) == 1
import time from scheduling.Scheduler import * start = time.time() scheduler = Scheduler() scheduler.run() scheduler.print_history() scheduler.visualize_history() end = time.time() print("Elapsed time: %f s" % (end - start)) # # example for using visualize_history_file() history = read_json_file("scheduling_history.json") visualize_history_file(history) #calculate group average response time from history file group_response_time = get_group_avg_response_time(history) print(group_response_time) #Elapsed time: 381.918144 s #[10846.0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
class TestScheduler(unittest.TestCase): # Arrange def setUp(self): self.pcb1 = PCB(3, 5, 10) self.pcb2 = PCB(2, 5, 10) self.pcb3 = PCB(1, 5, 10) self.scheduler = Scheduler() def test_whenISetTheSchedulerPolicyAsFifo_thenIThePCBsAsThePriorityStates( self): self.scheduler.set_as_fifo() self.scheduler.add(self.pcb1) self.scheduler.add(self.pcb2) self.scheduler.add(self.pcb3) self.assertEquals(self.scheduler.next(), self.pcb1) self.assertEquals(self.scheduler.next(), self.pcb2) self.assertEquals(self.scheduler.next(), self.pcb3) def test_whenISetTheSchedulerPolicyAsPriority_thenIThePCBsAsThePriorityStates( self): self.scheduler.set_as_pq() self.pcb1.set_priority(PCBPriorities().getPriorities().LOW) self.pcb2.set_priority(PCBPriorities().getPriorities().MEDIUM) self.pcb3.set_priority(PCBPriorities().getPriorities().HIGH) self.scheduler.add(self.pcb1) self.scheduler.add(self.pcb2) self.scheduler.add(self.pcb3) self.assertEquals(self.scheduler.next(), self.pcb3) self.assertEquals(self.scheduler.next(), self.pcb2) self.assertEquals(self.scheduler.next(), self.pcb1) def test_whenISetTheSchedulerPolicyAsRR_thenIThePCBsAsThePriorityStates( self): self.scheduler.set_as_rr(5) self.scheduler.add(self.pcb1) self.scheduler.add(self.pcb2) self.scheduler.add(self.pcb3) self.assertEquals(self.scheduler.next(), self.pcb1) self.assertEquals(self.scheduler.next(), self.pcb2) self.assertEquals(self.scheduler.next(), self.pcb3)