def main(): gc.collect() # process_requirements() gc.collect() loop = asyncio.get_event_loop() try: asyncio.ensure_future(wlog.logger.start()) log('Logger initialized') try: loop.run_until_complete(run()) except Exception as e: log(exception_traceback_string(e)) while True: try: loop.run_forever() except Exception as e: log(exception_traceback_string(e)) loop.close() except Exception as e: log(exception_traceback_string(e)) try: log('Restarting esp as asyncio loop closed') except: pass machine.reset()
def test_fcn(request, response): async def count(): for i in range(10): print(i) await asyncio.sleep(1) asyncio.ensure_future(count()) yield from jsonify(response, {'data': 'test function'})
def __init__(self, host: str, wifi_network): self.host = host self.wifi_network = wifi_network self.con = None # setup lock self.com_lock = Lock() asyncio.ensure_future(self.connect_mqtt())
def cmd(request, response): yield from request.read_form_data() print(request.form) try: action = request.form['action'] if action == 'open': asyncio.ensure_future(door.open()) elif action == 'close': asyncio.ensure_future(door.close()) except KeyError: pass yield from jsonify(response, {'action': action})
async def main(): # get configuration from file with open("configuration.json") as f: conf = json.load(f) # wifi is not connected we just got the coroutine wifi = connect_wifi(ssid=conf["network"]["ssid"], passphrase=conf["network"]["passphrase"]) # mqtt mqtt = MQTTCommunicator(host=conf["mqtt"]["host"], wifi_network=wifi) # sensor for sensor in conf["sensors"]: if sensor["class"] not in sensor_class: print("Sensor class %s not available" % (sensor["class"])) continue print("Activate sensor %s (%s)" % (sensor['name'], sensor['class'])) s = sensor_class[sensor["class"]](sensor, communicator=mqtt) asyncio.ensure_future(s.run())
def test_non_blocking_asyncio(): ''' creates two buttons (pin 18 and 19) then it starts the async loop adds handler for both buttons in the loop adds tick_tock to the loop to show its running ''' pin18 = machine.Pin(pin_btn01, machine.Pin.IN, machine.Pin.PULL_UP) pin19 = machine.Pin(pin_btn02, machine.Pin.IN, machine.Pin.PULL_UP) loop = asyncio.get_event_loop(runq_len=40, waitq_len=40) asyncio.ensure_future(test_btn(pin18)) asyncio.ensure_future(test_btn(pin19)) asyncio.ensure_future(async_tick_tock()) loop.run_forever()
async def run(): log('All initialized') try: asyncio.ensure_future(notify_memory()) import app asyncio.ensure_future(app.main()) import discovery asyncio.ensure_future( discovery.DiscoveryServer(local_ip=wifi.get_ip()).start()) except Exception as e: log(exception_traceback_string(e), important=True) while True: await asyncio.sleep(1)
def open_cmd(request, response): print('close request') asyncio.ensure_future(door.close()) yield from jsonify(response, {'action': 'closing'})
def open_cmd(request, response): print('open request') asyncio.ensure_future(door.open()) yield from jsonify(response, {'action': 'opening'})
def start_server(self, *args, **kwargs): asyncio.ensure_future(self.make_server(*args, **kwargs), loop=self.loop)
def write(self, message, end='\n', flush=False, important=False): print(message, flush=flush, end=end) asyncio.ensure_future(self.awrite(message, end, flush, important), loop=self.loop)