One big gap is that its not showing responding to requests. This is fine for posts/puts, fire & forget requests, but If you have users requesting data/etc how will it return the request? You're returning the response before the task completes.
@@SoumilShah I don't understand what a queue gives you. You've already closed your connection to the client with your response. Can you give a pastebin for what you mean? Unless there's a callback url or something I don't see how you send data back.
Hope your back gets better. I was thinking If there isn't a way to make threading using decorators. When thread fails, does It return exception that se can deal with? I find Very difficult to understand why is not a good practice kill threads. Thanks for posting videos. I always learn a lot with you.
Junaid saif well actually yes you cannot kill threads that right regarding decorator I think it’s a great idea to make a decorator but thing is we want to run some function which is inside a request I have to use meta class to extract that function ie say task function that I was using in example video it’s something we can do I won’t recommend it
Are you doing a web scraping? Using selenium with flask to do that is not a good idea, The sulotion is by adding a python DLL to your heroku app. Lock at => ruclips.net/video/Ven-pqwk3ec/видео.html&ab_channel=AndresSevilla
You are still not clear with the concepts. Somewhere in the video you said "execute parallel". There's a difference between concurrency and parallelism. Python has gil. It excutes on 1 core. Execution cannot and does not happen in parallel.
HI Soumil, How can we know the status of the background thread, Like If when the sleep time is over in your given example can I get any validation on the webpage? with which I'll know that yes now this particular task is complete.
async is much better solution than this. Never ever create custom threads in any web frameworks because of the risk of having then as zombies and many more reasons. Thanks for the video
Does the thread also contains the flask context i.e. would I be able to use the Request method in Flask to retrieve the context set for flask. Will this also preserve the contexts under logging frameworks.
Thank you for the video, do you have a source for what you mentioned at the end? I have an app that I integrated with api. I send a message to it and it analyzes it and then returns a result t, but when I send many messages at the same time, it does not go well and you get a mess
if def task() is inside the route(/task) then it's not working. At first task( ) executes then class. Problem is 👇 But i have for loop inside task( ) and it's running almost 1 hour not in background. Have u solution?
Hey bro, How to properly terminate a flask web application that was launched in a separate thread, When I press +, something is not being terminated and the script never stops. following is the code that i am using Quick response need please from flask import Flask, jsonify, render_template, request import webbrowser import time import random import json import threading app = Flask(__name__) @app.route('/_stuff', methods = ['GET']) def stuff(): Senesor_Data = { "ShotNum" : raw sensor data } time.sleep(2) return jsonify(result=json.dumps(Senesor_Data)) @app.route('/') def index(): return render_template('main.html') def flaskThread(): app.run(host='0.0.0.0', port=80, debug=False, use_reloader=False) #start in flask thread threading.Thread(target=flaskThread).start()
Hey soumil, thanks for this amazing video. I have just started flask. Wanted to know if we can automatically redirect to login page once the cookie gets expired in flask. Is there a way to implement this.
Thanks soumil for the reply. I had set the cookie timeout its working fine. Issue is that i am Flashing message of cookie expired when it sees no cookie exist after timeout.. but it is showing that flash error while launching the website as well. Ideally it shouldn’t flash that message while launch of localhost server right. I am using session cookie. Can you suggest some solution if i can somehow set the cookie while putting localhost in my browser rather than setting up the cookie on redirect.
@@SoumilShah thanks I was expecting the same , actually I m in nodejs was thinking to change my language to python because or concurrency and computation but I found that node js do concurrency with worker library
One big gap is that its not showing responding to requests. This is fine for posts/puts, fire & forget requests, but If you have users requesting data/etc how will it return the request? You're returning the response before the task completes.
In that case you put in queue call join and return data from queue
@@SoumilShah I don't understand what a queue gives you. You've already closed your connection to the client with your response. Can you give a pastebin for what you mean? Unless there's a callback url or something I don't see how you send data back.
Kelvin M well no if you close your connection then it’s not possible unless we have a callback
@@SoumilShah Hi, can you show sample or video with the queue?
if you want passing parameter simply use args=[your_param]
Thank you for this video man! Really helpful. I would love to see a similar one using Celery.
If we want to stop the thread .So how can we achieve it?
Hey.. @soumilshah1995 If I want to execute a function in the end after sending response to frontend.
Hrithik Raj Prasad well it would be background process as a thread
Hope your back gets better. I was thinking If there isn't a way to make threading using decorators. When thread fails, does It return exception that se can deal with? I find Very difficult to understand why is not a good practice kill threads. Thanks for posting videos. I always learn a lot with you.
Hi can you use db operation in thread. I have been trying but without any luck
Junaid saif well actually yes you cannot kill threads that right regarding decorator I think it’s a great idea to make a decorator but thing is we want to run some function which is inside a request I have to use meta class to extract that function ie say task function that I was using in example video it’s something we can do I won’t recommend it
@@SoumilShah thanks for the video. do you know why it is not recommended or bad practice to kill your program/ app while the thread is running?
Looks like this doesn't work when the application is started with gunicorn? :(
Are you doing a web scraping? Using selenium with flask to do that is not a good idea, The sulotion is by adding a python DLL to your heroku app. Lock at => ruclips.net/video/Ven-pqwk3ec/видео.html&ab_channel=AndresSevilla
Just awesome bro!!
Keep it up.....
you provide such valuable information, thanks man
Wonderful! Thank you very much!
You are still not clear with the concepts. Somewhere in the video you said "execute parallel". There's a difference between concurrency and parallelism. Python has gil. It excutes on 1 core. Execution cannot and does not happen in parallel.
thank you so much
HI Soumil,
How can we know the status of the background thread,
Like If when the sleep time is over in your given example can I get any validation on the webpage? with which I'll know that yes now this particular task is complete.
i m have same query, did you found the solution
How to get the ID of the running thread? If it is taking too long, the user might wish to cancel the task. Is there a solution?
why do you want to get the ID usually process has PID and not Threads ;d
In AWS serverless application, we cannot use threads so how can we use asynchronous approach?
async is much better solution than this. Never ever create custom threads in any web frameworks because of the risk of having then as zombies and many more reasons. Thanks for the video
Thank you so much Soumil!!
hey thanks man, its really helpful
Thank You
for me flask_restful gives an error. I have flask version 2.0>
Does the thread also contains the flask context i.e. would I be able to use the Request method in Flask to retrieve the context set for flask. Will this also preserve the contexts under logging frameworks.
Using concurrent futures is a better idea here I think
whats the difference between threading and _thread
thanks
thanks it helped me.
What if i chage the route ??
can you show how to the same using concurrent.futures.ThreadPoolExecutor as threads create lot of GC if called repeatedly forever.
Thank you for the video, do you have a source for what you mentioned at the end?
I have an app that I integrated with api. I send a message to it and it analyzes it and then returns a result t, but when I send many messages at the same time, it does not go well and you get a mess
Velcome to my comment sir, please remake video in Hindi.
Hi Soumil,
Thanks for the video.I have a query,if 1000+ user is accessing our flask api at same time then will the above solution works?
Yes if it’s background process and you can scale up using docker swarm
nice
if def task() is inside the route(/task) then it's not working. At first task( ) executes then class.
Problem is 👇
But i have for loop inside task( ) and it's running almost 1 hour not in background.
Have u solution?
I don't think this is right video. He doesn't know
Nice vidio
Hey bro, How to properly terminate a flask web application that was launched in a separate thread, When I press +, something is not being terminated and the script never stops. following is the code that i am using Quick response need please
from flask import Flask, jsonify, render_template, request
import webbrowser
import time
import random
import json
import threading
app = Flask(__name__)
@app.route('/_stuff', methods = ['GET'])
def stuff():
Senesor_Data = {
"ShotNum" : raw sensor data
}
time.sleep(2)
return jsonify(result=json.dumps(Senesor_Data))
@app.route('/')
def index():
return render_template('main.html')
def flaskThread():
app.run(host='0.0.0.0', port=80, debug=False, use_reloader=False)
#start in flask thread
threading.Thread(target=flaskThread).start()
how to deal this in. production? ie with gunicorn and ngonx
Well when you run in docker container with uwsgi it will take care of it )
Watch my deployment videos
Hi Soumil,
How can I call methods from a class to read and write a file using flask?
Create a instance and call the method )
Hey soumil, thanks for this amazing video. I have just started flask. Wanted to know if we can automatically redirect to login page once the cookie gets expired in flask. Is there a way to implement this.
Flask login manager is something you want to look for )
Thanks soumil for the reply. I had set the cookie timeout its working fine. Issue is that i am
Flashing message of cookie expired when it sees no cookie exist after timeout.. but it is showing that flash error while launching the website as well. Ideally it shouldn’t flash that message while launch of localhost server right. I am using session cookie. Can you suggest some solution if i can somehow set the cookie while putting localhost in my browser rather than setting up the cookie on redirect.
Hey soumil is
There a way to set cookie through flask session right after you hit localhost in your browser and just before redirect to login screen
Not working on Gunicorn, heroku
What error are you getting? Did you originally try this out due to an API call that took long and would sometimes work but sometimes not?
eventloop vs parallel thread which one is good ?
At end of day they do same thing )
@@SoumilShah thanks I was expecting the same , actually I m in nodejs was thinking to change my language to python because or concurrency and computation but I found that node js do concurrency with worker library
@@stepup8108 yes or you fledge the work through Api on python language if you know what I am talking about
@@SoumilShah got it , but my background is javascript so more inclined to nodejs rather than python
@@stepup8108 you can always fan out worker on aws lambda to write your code on lambda call the lambda from node )