liveBook · Manning

Polling

As explained in Build Your Own Robot: Using Python, CRICKIT, and Raspberry Pi

Polling is a technique that repeatedly checks a resource or condition at a configured interval.

Polling repeatedly checks a resource or condition at a configured interval. For a changing image file, compare its current modification time with the previous value, report whether it changed, then pause before checking again. section 10.4.2

A reusable file-polling loop typically:

  • Stores the path and initializes the previous modification time. section 10.4.2
  • Reads the current modification time and compares it with the stored value. section 10.4.2
  • Updates the stored value, handles new data when a change is detected, and sleeps for the polling interval. section 10.4.5

Polling a live camera image

A camera saves frames to robo_stream.jpg at 30 frames per second. A FileWatcher polls the file at 60 checks per second, producing alternating changed and unchanged results as new frames arrive. A Tornado handler reads changed JPEG images and flushes them to a browser as Motion JPEG. section 10.4.2 section 10.4.5

class FileWatcher:
    def __init__(self, path):
        self.path = path
        self.last_mtime = None
 
    def has_changed(self):
        mtime = getmtime(self.path)
        last_mtime = self.last_mtime
        self.last_mtime = mtime
        return (mtime != last_mtime)

Browse the FileWatcher implementation that compares modification times and returns a change flag. section 10.4.2

How Build Your Own Robot: Using Python, CRICKIT, and Raspberry Pi covers it

How deep it goes
Explains a reusable watcher, modification-time comparison, loop timing, and web-stream integration without deeper scheduling internals.
Examples
Demonstrates watching robo_stream.jpg and forwarding changed JPEG frames to a browser at a 60-check-per-second interval.
What it assumes
Assumes a saved camera stream and basic Python files, modules, loops, command-line arguments, and asynchronous handlers.
What you can do after
Build it from scratch as a Python FileWatcher and apply it to camera-file monitoring or browser video streaming.
How it connects
Connects filesystem monitoring with file modification time, camera streaming, Motion JPEG, Tornado, and asynchronous programming.

Also explained in

Compare how 9 books explain polling →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Your Own Robot: Using Python, CRICKIT, and Raspberry Pi. The section links point to the parts of the book it draws on.