async read keypressed in terminal

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
VladVons
Posts: 43
Joined: Sun Feb 12, 2017 6:49 pm
Location: Ukraine

async read keypressed in terminal

Post by VladVons » Fri Feb 14, 2020 6:57 pm

i want to read in terminal keypressed while http server is running.
please hint me

User avatar
jimmo
Posts: 1848
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: async read keypressed in terminal

Post by jimmo » Sat Feb 15, 2020 5:02 am

VladVons wrote:
Fri Feb 14, 2020 6:57 pm
i want to read in terminal keypressed while http server is running.
You can use select or poll on sys.stdin to see if there are characters available (which you can then read using sys.stdin.read).

More info here: viewtopic.php?f=18&t=6954&p=39455

VladVons
Posts: 43
Joined: Sun Feb 12, 2017 6:49 pm
Location: Ukraine

Re: async read keypressed in terminal [SOLVED]

Post by VladVons » Sat Feb 15, 2020 8:27 am

thanks jimmo

Code: Select all

import uasyncio as asyncio
import sys
import select


async def Task1():
  while True:
    print('Task1')
    await asyncio.sleep(1)

async def Task2():
  while True:
    await asyncio.sleep(0.1)
    while sys.stdin in select.select([sys.stdin], [], [], 0)[0]:
        ch = sys.stdin.read(1)
        print('none block input char', ch)


loop = asyncio.get_event_loop()
loop.create_task(Task1())
loop.create_task(Task2())
loop.run_forever()

Post Reply