adafruit / circuitpython

CircuitPython - a Python implementation for teaching coding with microcontrollers
https://circuitpython.org
Other
4.13k stars 1.22k forks source link

Auto-reload works only occasionally on file save #3528

Closed todbot closed 4 years ago

todbot commented 4 years ago

Host PC: Macbook Pro running macOS 10.15.7 Board: QT Py, with no peripherals (also happens on Trinket M0) CircuitPython version: 6-beta2

I've been noticing in CircuitPython 6 that the auto-reload doesn't seem to work much after saving a file. For instance, in the CP program below (which plays random colors on the built-in Neopixel and prints out the current time.monotonic() and a string), I can edit the "hello" string, save the file, and auto-reload does not happen.

import board
import time
import neopixel
from random import randint
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
while True:
    print(time.monotonic(), "hello")
    pixel.fill( (randint(0,255), randint(0,255), randint(0,255) ))
    time.sleep(0.2)

Here's a screencast showing what I'm seeing.
circuitpython6beta-noreload

(also at https://imgur.com/gallery/zAHgrNg)

In the screencast, there are two terminals: the bottom one shows the output of CP's USB Serial, in the top one I use vim to edit code.py, save it, and quit the editor. In the serial terminal, nothing changes, until I Ctrl-C and Ctrl-D to stop & restart execution.

educ8s commented 4 years ago

I also face a similar problem. I am using a Seeeduino Xiao board. When using the latest CircuitPython 6.0.0 Beta 2 the program does not autoload when saving. Any program, even the blink program does not run on save. I have to disconnect and reconnect the board in order for the program to run. When using CircuitPython 5.3.1 everything works as expected.

tannewt commented 4 years ago

Anyone tried this on an M0 Express board?

gamblor21 commented 4 years ago

I just tried this on an M0 Basic (and just changed the print as no neopixel) and saw the exact same behavior. Hitting Ctrl-S in Mu did not auto-reload. Ctrl-C & Ctrl-D reloaded it.

tannewt commented 4 years ago

Ok, I suspect that this is an interaction with the time.sleep and the autoreload timing.

todbot commented 4 years ago

ahh, yes! If I create a similar small script, but do not use time.sleep(), auto-reload works fine. E.g.

import board
import time
import neopixel
from random import randint
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
lasttime = time.monotonic()
while True:
    if time.monotonic() - lasttime > 0.2:
        lasttime=time.monotonic()
        print(time.monotonic(), "hello")
        pixel.fill( (randint(0,255), randint(0,255), randint(0,255) ))
todbot commented 4 years ago

Verified on Trinket M0 that #3532 fixes auto-reload.