Hello world! I'm trying to help out a friend with this problem that I couldn't figure out. If the following code is being run locally, Flask immediately returns the response and a few seconds later "Task done" appears in the logs. This makes sense for a quick file IO or database action, for example, that doesn't change the Flask response to the user. So we don't want to keep the user waiting. However, on PythonAnywhere, this example code blocks - rendering the idea of the background task useless. The local test also used only 1 worker and no threading. Any ideas? Is this a bug or an intended limitation of PythonAnywhere? Thanks in advance!
from flask import Flask
from multiprocessing import Process
from time import sleep
app = Flask(__name__)
def task():
# Write some data, takes a few seconds
sleep(5)
print("Task done")
@app.route("/")
def hello():
# A quick task that we don't need to wait for
Process(target=task).start()
# The response can be sent right away
return "Hello World"