Polling as fallback helps your web app check for updates regularly when real-time methods don't work. It keeps the app updated by asking the server often.
Polling as fallback in Flask
import time from flask import Flask, jsonify, request app = Flask(__name__) @app.route('/poll') def poll(): # Return latest data or status data = {'message': 'Hello from server', 'time': time.time()} return jsonify(data) if __name__ == '__main__': app.run(debug=True)
The server provides an endpoint that the client calls repeatedly.
The client uses JavaScript to request this endpoint at intervals.
import time from flask import Flask, jsonify app = Flask(__name__) @app.route('/poll') def poll(): return jsonify({'status': 'ok', 'time': time.time()})
import time from flask import Flask, jsonify, request app = Flask(__name__) @app.route('/poll') def poll(): last_check = float(request.args.get('last_check', 0)) current_time = time.time() if current_time - last_check > 5: return jsonify({'update': True, 'time': current_time}) else: return jsonify({'update': False})
This Flask app creates a /poll endpoint that returns a message and the current server time. A client can call this endpoint repeatedly to get updates.
import time from flask import Flask, jsonify app = Flask(__name__) @app.route('/poll') def poll(): data = {'message': 'Hello from server', 'time': time.strftime('%H:%M:%S')} return jsonify(data) if __name__ == '__main__': app.run(debug=True)
Polling can cause more server load because it sends many requests.
Choose a reasonable interval (like every 5 seconds) to avoid overload.
Use polling as a fallback only when real-time methods are not possible.
Polling asks the server for updates regularly by sending repeated requests.
It is simple but less efficient than real-time communication.
Use polling as a backup when other methods fail or are unsupported.
Practice
Solution
Step 1: Understand polling purpose
Polling sends repeated requests to the server to check for updates.Step 2: Identify fallback use
Polling is used when real-time communication methods like WebSockets are unavailable or fail.Final Answer:
To regularly ask the server for updates when real-time methods fail -> Option AQuick Check:
Polling = fallback for updates [OK]
- Thinking polling reduces server requests
- Confusing polling with server push
- Believing polling stops communication
Solution
Step 1: Check Flask imports and response format
Using jsonify is the correct way to return JSON responses in Flask.Step 2: Verify route and function syntax
from flask import Flask, jsonify app = Flask(__name__) @app.route('/poll') def poll(): return jsonify({'status': 'ok'}) uses @app.route and returns jsonify({'status': 'ok'}) correctly.Final Answer:
Correct Flask polling endpoint returning JSON with jsonify -> Option DQuick Check:
jsonify for JSON response = from flask import Flask, jsonify app = Flask(__name__) @app.route('/poll') def poll(): return jsonify({'status': 'ok'}) [OK]
- Returning plain string instead of JSON
- Using json.dumps without Response object
- Returning dict directly without jsonify
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/poll')
def poll():
data = {'message': 'Hello', 'count': 5}
return jsonify(data)Solution
Step 1: Understand jsonify output
jsonify converts Python dict to JSON string with double quotes and correct syntax.Step 2: Check returned data format
The returned JSON string will be {"message": "Hello", "count": 5} as a JSON response.Final Answer:
{"message": "Hello", "count": 5} -> Option BQuick Check:
jsonify returns JSON string = {"message": "Hello", "count": 5} [OK]
- Expecting plain text instead of JSON
- Confusing Python dict syntax with JSON
- Assuming jsonify is undefined
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/poll')
def poll():
data = {'status': 'ok'}
Solution
Step 1: Check for return statement
The poll() function defines data but does not return it.Step 2: Verify function completeness
Missing return statement means the endpoint returns None by default, causing unexpected empty response.Final Answer:
Function poll() is missing a return statement -> Option AQuick Check:
Missing return in view function [OK]
- Missing return statement in route function
- Forgetting route decorator
- Not creating Flask app instance
Solution
Step 1: Understand fallback strategy
Fallback means trying WebSocket first, then polling if WebSocket fails or is unsupported.Step 2: Identify correct client-server interaction
Client tries WebSocket; if it fails, client sends repeated requests to '/poll' endpoint every few seconds.Final Answer:
Use WebSocket for updates; if unsupported, client polls '/poll' endpoint every 5 seconds -> Option CQuick Check:
Fallback = WebSocket then polling [OK]
- Ignoring fallback and using only polling
- Using polling without client requests
- Running both methods without fallback logic
Ready to prove it?
Pass the test and this pattern moves unknown
