Polling is a technique where the client repeatedly asks the server for updates at regular intervals.
Click to reveal answer
beginner
Why use polling as a fallback in Flask apps?
Polling is used as a fallback when real-time communication methods like WebSockets are not supported or fail, ensuring the client still receives updates.
Click to reveal answer
intermediate
How does polling work in a Flask app?
The client sends repeated HTTP requests to a Flask route that returns the latest data. The client waits a set time between requests.
Click to reveal answer
intermediate
What is a downside of polling compared to WebSockets?
Polling can cause more network traffic and latency because it sends requests even when no new data is available.
Click to reveal answer
intermediate
How can you implement polling in a Flask frontend?
Use JavaScript's setInterval to send fetch requests to a Flask endpoint periodically and update the page with the response.
Click to reveal answer
What does polling do in a Flask web app?
AKeeps a permanent connection open
BPushes data from server to client automatically
CRepeatedly requests data from the server
DBlocks the server until data changes
✗ Incorrect
Polling means the client asks the server repeatedly for updates.
Why might polling be used as a fallback?
ABecause WebSockets might not be supported
BBecause polling is faster than WebSockets
CBecause polling uses less network traffic
DBecause polling requires no client code
✗ Incorrect
Polling is a fallback when real-time methods like WebSockets are unavailable.
Which JavaScript function is commonly used to implement polling?
AclearInterval
BsetInterval
CsetTimeout
DaddEventListener
✗ Incorrect
setInterval runs code repeatedly at fixed time intervals, perfect for polling.
What is a disadvantage of polling?
AIt blocks the browser UI
BIt requires a WebSocket server
CIt only works on mobile devices
DIt can cause unnecessary network requests
✗ Incorrect
Polling sends requests even if no new data is available, causing extra traffic.
In Flask, polling endpoints usually return what type of data?
AJSON with the latest updates
BHTML pages only
CBinary files
DCSS stylesheets
✗ Incorrect
Polling endpoints typically return JSON data for easy client updates.
Explain how polling works as a fallback method in a Flask web application.
Think about how the client keeps asking the server for updates.
You got /4 concepts.
Describe the pros and cons of using polling compared to WebSockets in Flask apps.
Consider speed, network use, and compatibility.
You got /4 concepts.
Practice
(1/5)
1. What is the main reason to use polling as a fallback in Flask applications?
easy
A. To regularly ask the server for updates when real-time methods fail
B. To reduce the number of server requests to zero
C. To make the server push updates automatically without client requests
D. To avoid any communication between client and server
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 A
Quick Check:
Polling = fallback for updates [OK]
Hint: Polling means repeated requests when real-time fails [OK]
Common Mistakes:
Thinking polling reduces server requests
Confusing polling with server push
Believing polling stops communication
2. Which Flask code snippet correctly implements a simple polling endpoint that returns JSON data?
easy
A. from flask import Flask
app = Flask(__name__)
@app.route('/poll')
def poll():
return {'status': 'ok', 'time': datetime.now()}
B. from flask import Flask
app = Flask(__name__)
@app.route('/poll')
def poll():
return 'status: ok'
C. from flask import Flask, json
app = Flask(__name__)
@app.route('/poll')
def poll():
return json.dumps({'status': 'ok'})
D. from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/poll')
def poll():
return jsonify({'status': 'ok'})
Solution
Step 1: Check Flask imports and response format
Using jsonify is the correct way to return JSON responses in Flask.
Hint: jsonify returns JSON string with double quotes [OK]
Common Mistakes:
Expecting plain text instead of JSON
Confusing Python dict syntax with JSON
Assuming jsonify is undefined
4. Identify the error in this Flask polling endpoint code:
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/poll')
def poll():
data = {'status': 'ok'}
medium
A. Function poll() is missing a return statement
B. Missing @app.route decorator
C. Flask app is not created properly
D. Returning a dict directly without using jsonify causes an error
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 A
Quick Check:
Missing return in view function [OK]
Hint: Always include return statement in Flask route handlers [OK]
Common Mistakes:
Missing return statement in route function
Forgetting route decorator
Not creating Flask app instance
5. You want to implement polling as a fallback in a Flask app that first tries WebSocket for real-time updates. Which approach correctly combines both methods?
hard
A. Use WebSocket and polling simultaneously without fallback logic
B. Only use polling and ignore WebSocket support
C. Use WebSocket for updates; if unsupported, client polls '/poll' endpoint every 5 seconds
D. Use polling only on the server side without client requests
Solution
Step 1: Understand fallback strategy
Fallback means trying WebSocket first, then polling if WebSocket fails or is unsupported.