0
0
Flaskframework~20 mins

Why background processing matters in Flask - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Background Processing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a Flask route performs a long task synchronously?

Consider a Flask route that processes a long task directly in the request handler. What is the user experience during this time?

Flask
from flask import Flask
import time

app = Flask(__name__)

@app.route('/long-task')
def long_task():
    time.sleep(10)  # Simulates a long task
    return 'Task complete!'
AThe user gets an error message because Flask does not support time.sleep().
BThe user immediately sees 'Task complete!' and the task runs in the background.
CThe server crashes because it cannot handle long tasks.
DThe user waits for 10 seconds with no response until the task finishes, then sees 'Task complete!'.
Attempts:
2 left
💡 Hint

Think about what happens when the server is busy doing one thing.

state_output
intermediate
2:00remaining
What is the output when using a background task with Flask and Celery?

Given a Flask app using Celery to run a background task, what will the user see immediately after triggering the task?

Flask
from flask import Flask, jsonify
from celery import Celery

app = Flask(__name__)
app.config['CELERY_BROKER_URL'] = 'redis://localhost:6379/0'
celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])

@celery.task
def background_task():
    import time
    time.sleep(5)
    return 'Done'

@app.route('/start-task')
def start_task():
    task = background_task.delay()
    return jsonify({'task_id': task.id, 'status': 'started'})
A{"task_id": "<some_id>", "status": "started"}
B"Done"
CThe server crashes due to missing Redis server.
DThe user waits 5 seconds and then sees "Done".
Attempts:
2 left
💡 Hint

Background tasks let the server respond immediately.

🔧 Debug
advanced
2:00remaining
Why does this Flask app freeze on multiple long requests?

Examine the Flask app below. Why does it freeze or become unresponsive when multiple users access the /process route simultaneously?

Flask
from flask import Flask
import time

app = Flask(__name__)

@app.route('/process')
def process():
    time.sleep(10)  # Simulate long processing
    return 'Done'

if __name__ == '__main__':
    app.run()
AThe app runs fine; freezing is caused by client-side issues.
BFlask's default server handles one request at a time, so concurrent requests wait and cause freezing.
CThe time.sleep(10) causes a syntax error, freezing the app.
DFlask automatically runs each request in a new thread, so freezing is impossible.
Attempts:
2 left
💡 Hint

Think about how Flask's built-in server handles requests.

🧠 Conceptual
advanced
2:00remaining
Why use background processing in web apps?

Which of the following is the best reason to use background processing in a Flask web app?

ATo improve user experience by handling long tasks without blocking the main server thread.
BTo avoid writing any code for handling requests.
CTo make the app run slower and use more memory.
DTo prevent users from accessing the app during maintenance.
Attempts:
2 left
💡 Hint

Think about what users feel when waiting for slow tasks.

📝 Syntax
expert
3:00remaining
Which Flask code snippet correctly starts a background task using threading?

Choose the code that correctly starts a background task in Flask using Python's threading module without blocking the main request.

Flask
from flask import Flask
import threading
import time

app = Flask(__name__)

def background_task():
    time.sleep(5)
    print('Background task done')

@app.route('/start')
def start():
    # Which option correctly starts the background task?
    pass
A
threading.start_new_thread(background_task)
return 'Task started!'
B
background_task()
return 'Task started!'
C
threading.Thread(target=background_task).start()
return 'Task started!'
D
threading.Thread(background_task()).start()
return 'Task started!'
Attempts:
2 left
💡 Hint

Remember how to create and start a thread properly.