0

I'm logging sensor data in a while loop and storing it in a variable.

There's a delay of 1 second between sensor reads.

I'd like to allow a user to input annotations while the sensor is logging, however these might happen at arbitrary times and I don't want to interrupt the loop while I wait for an input. If there's no input I'd like to "remember" the previous one and use that value.

The code looks something like this:

while True:
    sensor_reading = readSensor()
    annotation = input('Input your notes: ')
    dataPoint = {'Reading':sensor_reading, 'Annotation':annotation}
    dataList.append(dataPoint)

An example of how the data might look is as follows. (Notes were added in seconds 1, 4 & 6)

Time(s)   Reading    Annotation
   1        250      Notes 1... 
   2        247      Notes 1...
   3        252      Notes 1...
   4        254      Notes 2...
   5        249      Notes 2...
   6        251      Notes 3...

Here's what I'm trying to do with the asyncio module:

import asyncio
import json
import aioconsole

async def read_sensor():
    await readSensor()

async def add_notes():
    while True:
        notes = await aioconsole.ainput('Input your notes: ')
try:
    asyncio.get_event_loop().run_until_complete(asyncio.wait([
        read_sensor(),
        add_notes()
    ]))
except KeyboardInterrupt:
     quit()
ortunoa
  • 345
  • 4
  • 11
  • The `input` command freezes the main thread to wait for user input. So if you're not using threads, your code will get stuck there. You could use [asynchronous inputs](https://stackoverflow.com/questions/58454190/python-async-waiting-for-stdin-input-while-doing-other-stuff) to solve it. Or the python `threading` module to read the sensor on the background. – Carl HR Oct 05 '22 at 20:47

0 Answers0