def main() -> None: try: # Raises an HTTPError exception if moodle is not properly up get_moodlepage().raise_for_status() # Create a .env file if it doesn't exist if not file_exists(".env"): create_default_env_file() log_with_time("The '.env' file has been created") # Get our session set up user_credentials = get_env_netname_credentials() session = get_session(user_credentials) # Get the fetch_and_notify function running repeatedly log_with_time("Starting scheduler") scheduler = sched.scheduler() scheduler.enter(0, 1, fetch_and_notify, (session, scheduler)) scheduler.run() except HTTPError as e: log_with_time(f"HTTPError occured (status code is 4xx or 5xx):\n{e}") except (ConnectionError, Timeout) as e: log_with_time( f"Moodle website appears to be down (ConnectionError or Timeout):\n{e}" ) except Exception as e: log_with_time(f"Exception:\n{e}") log_with_time( f"Rescheduling the main method to run in {get_scheduler_delay()} seconds" ) scheduler = sched.scheduler() scheduler.enter(get_scheduler_delay(), 1, main) scheduler.run()
def callback(scheduler=None): if scheduler is None: scheduler = sched.scheduler(time.time, time.sleep) scheduler.enter(0,1,callback,([scheduler])) scheduler.run() scheduler.enter(.1,1,callback,([scheduler])) if self.ser.inWaiting() > 0: a = self.ser.read() self.queue.put(a)
NUM_TESTERS = 50 SERVER_ADDR = ('localhost', 1234) FMT = b"TEST%05d" LEN = 9 async def echoclient(cid): sock = socket.socket() sock.setblocking(False) try: sock.connect(SERVER_ADDR) except BlockingIOError as e: pass for _ in range(10): await io(sock, selectors.EVENT_WRITE) testdata = FMT % cid sock.send(testdata) await io(sock, selectors.EVENT_READ) data = sock.recv(LEN) assert testdata == data, "Bad data: {} != {}".format(testdata, data) await sleep(0.2) sock.close() for cid in range(NUM_TESTERS): scheduler.add_coro(echoclient(cid)) scheduler.run()
'--enable_log', type=int, help='', required=False) args = parser.parse_args() if args.thingplug_host != None: THINGPLUG_HOST = args.thingplug_host if args.thingplug_port != None: THINGPLUG_PORT = args.thingplug_port if args.app_eui != None: THINGPLUG_APPEUI = args.app_eui if args.period_time != None: PERIOD_TIME = args.period_time if args.enable_log != None: args.enable_log = 1 thingplug = ThingPlug.ThingPlug(THINGPLUG_HOST, THINGPLUG_PORT) thingplug.login(args.user_id, args.user_pw) thingplug.setAppEui(args.app_eui) if PERIOD_TIME == 0: status, data, lt_time = thingplug.getLatestData( args.node_id, args.container) current_time = str(datetime.datetime.now()) output_data = current_time + ',' + data + ',' + lt_time + '\r\n' print output_data, else: while True: scheduler = sched.scheduler(time.time, time.sleep) param = (thingplug, args.node_id, args.container, args.enable_log) scheduler.enter(PERIOD_TIME, 1, fun_getLatestData, param) scheduler.run()
def cyclic_scan(scan_interval, scheduler): # 循环扫描 run_cleaning_tasks() scheduler.enter(scan_interval, 0, cyclic_scan, (scan_interval, scheduler)) scheduler.run()