Consider a Flask app that uses polling as a fallback to update client data. What will the client see if the polling interval is set too high?
from flask import Flask, jsonify import time app = Flask(__name__) last_update = time.time() data = {'value': 0} @app.route('/data') def get_data(): global last_update, data if time.time() - last_update > 5: data['value'] += 1 last_update = time.time() return jsonify(data)
Think about what happens if the client polls less often than the server updates.
If the polling interval on the client is longer than the server's update interval, the client will see stale data until the next poll happens.
Which Flask route code correctly implements a polling endpoint that returns an incrementing JSON counter?
Remember to declare global variables inside functions when modifying them.
Option D correctly declares count as global before modifying it. Option D and D cause UnboundLocalError. Option D modifies a mutable dict without global but does not use a counter variable.
Given this Flask polling endpoint, what is the value of data['value'] after 10 seconds if the client polls every 1 second?
from flask import Flask, jsonify import time app = Flask(__name__) last_update = time.time() data = {'value': 0} @app.route('/data') def get_data(): global last_update, data if time.time() - last_update > 3: data['value'] += 1 last_update = time.time() return jsonify(data)
Calculate how many 3-second intervals fit into 10 seconds.
The server increments data['value'] every 3 seconds. In 10 seconds, it increments 3 times (at ~3s, ~6s, ~9s). The client polling every 1 second will see these updates.
A Flask polling endpoint returns stale data even though the server updates it every second. What is the most likely cause?
from flask import Flask, jsonify import time app = Flask(__name__) last_update = time.time() data = {'count': 0} @app.route('/poll') def poll(): global last_update if time.time() - last_update > 1: data['count'] += 1 last_update = time.time() return jsonify(data)
Check variable scope inside the function.
Without declaring last_update as global, the assignment inside the function creates a local variable, so the global last_update never changes, causing stale data.
In a Flask web app, why might polling be used as a fallback method for updating client data instead of WebSockets?
Consider browser and network compatibility.
Polling is a fallback because it works universally, even when WebSockets are blocked by firewalls or unsupported by browsers. However, it is less efficient than WebSockets.