def delete_task(self, task): """ Delete a task from the task scheduler """ DLOG.debug("Pool %s: Delete Task, name=%s." % (self._task_worker_pool.name, task.name)) for timer_id, timer_owner in self._task_timers.items(): if timer_owner.id == task.id: timers.timers_delete_timer(timer_id) del self._task_timers[timer_id] for timer_id, timer_owner in self._task_work_timers.items(): if timer_owner.task_id == task.id: timers.timers_delete_timer(timer_id) del self._task_work_timers[timer_id] for select_obj, select_obj_owner in self._task_read_selobjs.items(): if select_obj_owner.id == task.id: selobj.selobj_del_read_obj(select_obj) del self._task_read_selobjs[select_obj] for select_obj, select_obj_owner in self._task_write_selobjs.items(): if select_obj_owner.id == task.id: selobj.selobj_del_write_obj(select_obj) del self._task_write_selobjs[select_obj] del self._tasks[task.id]
def task_work_complete(self): """ A task worker has completed it's assigned work """ while True: select_obj = (yield) worker = self._workers_selobj.get(select_obj, None) if worker is not None: self._task_worker_pool.release_worker(worker) selobj.selobj_del_read_obj(worker.selobj) del self._workers_selobj[worker.selobj] task_work = worker.get_task_work_result() if task_work is not None: for timer_id, timer_owner in self._task_work_timers.items( ): if timer_owner.id == task_work.id: timers.timers_delete_timer(timer_id) del self._task_work_timers[timer_id] del self._workers_timer[timer_id] task = self._tasks.get(task_work.task_id, None) if task is not None: self._running_task = task try: task.task_work_complete(task_work) except StopIteration: self.delete_task(task) if self._task_worker_pool.available_workers(): self.schedule_task_work()
def cancel_task_io_read_wait(self, select_obj, task): """ Cancel a task read selection object being waited on """ select_obj_owner = self._task_read_selobjs.get(select_obj, None) if select_obj_owner is not None: if select_obj_owner.id == task.id: selobj.selobj_del_read_obj(select_obj) del self._task_read_selobjs[select_obj]
def selobj_error_callback(self): while True: select_obj = (yield) client_connection = self._client_connections.get(select_obj, None) if client_connection is not None: selobj.selobj_del_read_obj(select_obj) del self._client_connections[select_obj] DLOG.info("Client connection error, from %s, port=%s." % (client_connection.ip, client_connection.port)) selobj.selobj_del_error_callback(select_obj)
def shutdown(self): """ Shutdown the TCP Server """ connections = self._client_connections.copy() for client_connection in connections.values(): selobj.selobj_del_read_obj(client_connection.selobj) selobj.selobj_del_error_callback(client_connection.selobj) client_connection.close() selobj.selobj_del_read_obj(self._connection.selobj) self._connection.close()
def closing_connection(self, select_obj): """ Connection is about to be closed """ client_connection = self._client_connections.get(select_obj, None) if client_connection is not None: selobj.selobj_del_read_obj(select_obj) selobj.selobj_del_error_callback(select_obj) del self._client_connections[select_obj] DLOG.verbose( "Client connection closing, ip=%s, port=%s, " "select_obj=%s." % (client_connection.ip, client_connection.port, select_obj))
def dispatch(self): while True: select_obj = (yield) if select_obj == self._connection.selobj: # Client Connect client_socket, client_address = self._socket.accept() client_ip = client_address[0] client_port = client_address[1] client_connection = TCPConnection(client_ip, client_port, client_socket, False, self, self._auth_key) selobj.selobj_add_read_obj(client_connection.selobj, self.dispatch) selobj.selobj_add_error_callback(client_connection.selobj, self.selobj_error_callback) self._client_connections[client_connection.selobj] \ = client_connection DLOG.verbose("Client connected from %s, port=%s." % (client_ip, client_port)) else: # Client Data client_connection = self._client_connections.get( select_obj, None) if client_connection is not None: msg = client_connection.receive(blocking=False) if msg is not None: DLOG.verbose("Message received from %s, port=%s, " "select_obj=%s." % (client_connection.ip, client_connection.port, select_obj)) self._message_handler(client_connection, msg) client_connection = self._client_connections.get( select_obj, None) if client_connection is not None: if client_connection.is_shutdown(): selobj.selobj_del_read_obj(select_obj) selobj.selobj_del_error_callback(select_obj) del self._client_connections[select_obj] DLOG.verbose( "Client connection closed, ip=%s, port=%s, " "select_obj=%s." % (client_connection.ip, client_connection.port, select_obj)) else: selobj.selobj_del_read_obj(select_obj) selobj.selobj_del_error_callback(select_obj)
def schedule_finalize(): """ Finalize the schedule module """ global _send_socket, _receive_socket, _pending_function_calls if _send_socket is not None: _send_socket.close() if _receive_socket is not None: selobj.selobj_del_read_obj(_receive_socket.fileno()) _receive_socket.close() del _pending_function_calls _pending_function_calls = list() # noqa: F841
def task_work_timeout(self): """ Work being done by the task has timed out """ timer_id = (yield) worker = self._workers_timer.get(timer_id, None) if worker is not None: self._task_worker_pool.timeout_worker(worker) selobj.selobj_del_read_obj(worker.selobj) del self._workers_selobj[worker.selobj] del self._workers_timer[timer_id] task_work = self._task_work_timers.get(timer_id, None) if task_work is not None: task = self._tasks.get(task_work.task_id, None) if task is not None: try: task.task_work_timeout(task_work) del self._task_work_timers[timer_id] except StopIteration: self.delete_task(task) if self._task_worker_pool.available_workers(): self.schedule_task_work()