Reputation: 79
As I understand, using Flask without Celery will block the server availability when a user starts a long operation.
My web server is actually not exposed to the internet and the maximum amount of users that will be connected at a time will be 3 (its of internal use for invoking automation scripts).
I have created a test environment in order to check how Flask is handling several calls at a time.
I have created 'task1' and 'task2' and run a loop with print statement + sleep in order to block the main thread for several seconds.
It seems like its not really blocking the main thead!!! I can run 'task1' start to see the output for every loop and then run 'task2' and see the output of 'task2' together with task one.
I checked the limit and it seems like I can run 7 tasks without blocking. How is that possible? according to the above, I dont need to use Celery in my organization since I will have no scenario that 2 users will run more then 2 tasks at a time.
Can someone explain why 'task1' is not blocking the starting of 'task2'?
@app.route('/task1', methods=['POST'])
def task1():
for i in range(4):
print('task 1 - ' + str(i))
time.sleep(1)
return 'message'
@app.route('/task2', methods=['POST'])
def task2():
for i in range(5):
print('task 2 - ' + str(i))
time.sleep(1)
return 'message'
<script >
function runTask(){
document.getElementById('task').value = "this is a value"
let req = $.ajax({
url : '/task1',
type : 'POST', // post request
data : { }
});
req.done(function (data) {
});
}
function runLongerTask(){
document.getElementById('longer_task').value = "this is longer value"
let req = $.ajax({
url : '/task2',
type : 'POST', // post request
data : { }
});
req.done(function (data) {
});
}
</script>
I expected 'task1' to start only when 'task2' will finish but it seems like the two tasks is running in threads (without actually configuring a thread) Here is the results that I got:
task 2 - 0
task 1 - 0
task 2 - 1
task 1 - 1
task 2 - 2
task 1 - 2
task 2 - 3
task 1 - 3
task 2 - 4
Upvotes: 0
Views: 1648
Reputation: 2903
As I understand, using Flask without Celery will block the server availability when a user starts a long operation.
This is not precisely correct, although it's a good rule of thumb to keep heavy workloads out of your webserver for lots of reasons.
You haven't described how you are running flask - with a WSGI container, or the run options. I'd look there to understand how concurrency is configured.
Upvotes: 0