Flask REST API with Threadding | Run task in background

Поделиться
HTML-код
  • Опубликовано: 22 авг 2024
  • Code pastebin.com/v...

Комментарии • 64

  • @kelvinm2088
    @kelvinm2088 3 года назад +3

    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
      @SoumilShah  3 года назад

      In that case you put in queue call join and return data from queue

    • @kelvinm2088
      @kelvinm2088 3 года назад

      @@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.

    • @SoumilShah
      @SoumilShah  3 года назад

      Kelvin M well no if you close your connection then it’s not possible unless we have a callback

    • @jist3349
      @jist3349 3 года назад

      @@SoumilShah Hi, can you show sample or video with the queue?

  • @FajarFebriano
    @FajarFebriano 2 года назад +1

    if you want passing parameter simply use args=[your_param]

  • @DesktopChronicles
    @DesktopChronicles 3 года назад

    Thank you for this video man! Really helpful. I would love to see a similar one using Celery.

  • @aartigiblogs
    @aartigiblogs 2 года назад +1

    If we want to stop the thread .So how can we achieve it?

  • @hrithikrajprasad4371
    @hrithikrajprasad4371 4 года назад +4

    Hey.. @soumilshah1995 If I want to execute a function in the end after sending response to frontend.

    • @SoumilShah
      @SoumilShah  3 года назад

      Hrithik Raj Prasad well it would be background process as a thread

  • @gilsonmiranda8372
    @gilsonmiranda8372 4 года назад +2

    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.

    • @junaidsaif2368
      @junaidsaif2368 4 года назад

      Hi can you use db operation in thread. I have been trying but without any luck

    • @SoumilShah
      @SoumilShah  4 года назад +1

      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

    • @michaels8297
      @michaels8297 4 года назад

      @@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?

  • @niklas_wa
    @niklas_wa 3 года назад +2

    Looks like this doesn't work when the application is started with gunicorn? :(

    • @i-7343
      @i-7343 3 года назад

      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

  • @shashiranjan7451
    @shashiranjan7451 2 года назад

    Just awesome bro!!
    Keep it up.....

  • @kspv2806
    @kspv2806 3 года назад

    you provide such valuable information, thanks man

  • @winter_t
    @winter_t Год назад

    Wonderful! Thank you very much!

  • @adityasethi9794
    @adityasethi9794 2 года назад

    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.

  • @MrSkelver
    @MrSkelver Месяц назад

    thank you so much

  • @sameerambre3131
    @sameerambre3131 3 года назад +2

    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.

  • @atuldeshpande5943
    @atuldeshpande5943 3 года назад +1

    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?

    • @SoumilShah
      @SoumilShah  3 года назад

      why do you want to get the ID usually process has PID and not Threads ;d

  • @user-gt3st3fc2c
    @user-gt3st3fc2c 11 месяцев назад

    In AWS serverless application, we cannot use threads so how can we use asynchronous approach?

  • @raju5081
    @raju5081 3 года назад

    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

  • @walterlensinas8756
    @walterlensinas8756 2 года назад

    Thank you so much Soumil!!

  • @TechWithUmraz
    @TechWithUmraz 3 года назад

    hey thanks man, its really helpful

  • @aldrinjenson
    @aldrinjenson Год назад

    Thank You

  • @amandubey5287
    @amandubey5287 2 года назад

    for me flask_restful gives an error. I have flask version 2.0>

  • @pallavirawat7128
    @pallavirawat7128 8 месяцев назад

    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.

  • @nonefvnfvnjnjnjevjenjvonej3384
    @nonefvnfvnjnjnjevjenjvonej3384 3 года назад

    Using concurrent futures is a better idea here I think

  • @decodeowl1351
    @decodeowl1351 3 года назад

    whats the difference between threading and _thread

  • @cyberninja80
    @cyberninja80 2 года назад

    thanks

  • @easytronics4183
    @easytronics4183 Год назад

    thanks it helped me.

  • @codeiton
    @codeiton 2 года назад

    What if i chage the route ??

  • @sathyarajshettigar
    @sathyarajshettigar Год назад

    can you show how to the same using concurrent.futures.ThreadPoolExecutor as threads create lot of GC if called repeatedly forever.

  • @maba7097
    @maba7097 Год назад

    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

  • @davidk7212
    @davidk7212 Год назад

    Velcome to my comment sir, please remake video in Hindi.

  • @surajprakash5227
    @surajprakash5227 2 года назад

    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?

    • @SoumilShah
      @SoumilShah  2 года назад

      Yes if it’s background process and you can scale up using docker swarm

  • @abdowakrimi3692
    @abdowakrimi3692 3 года назад +1

    nice

  • @si28sep
    @si28sep 3 года назад

    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?

    • @CS-zy6sw
      @CS-zy6sw 3 года назад

      I don't think this is right video. He doesn't know

  • @bijouteriesalim2392
    @bijouteriesalim2392 3 года назад +1

    Nice vidio

  • @hemanthkumar5982
    @hemanthkumar5982 3 года назад

    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()

  • @Nithin7860
    @Nithin7860 3 года назад

    how to deal this in. production? ie with gunicorn and ngonx

    • @SoumilShah
      @SoumilShah  3 года назад

      Well when you run in docker container with uwsgi it will take care of it )
      Watch my deployment videos

  • @ranjeevtiwari6976
    @ranjeevtiwari6976 3 года назад

    Hi Soumil,
    How can I call methods from a class to read and write a file using flask?

    • @SoumilShah
      @SoumilShah  3 года назад +1

      Create a instance and call the method )

  • @harkirankaur1137
    @harkirankaur1137 2 года назад

    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.

    • @SoumilShah
      @SoumilShah  2 года назад

      Flask login manager is something you want to look for )

    • @harkirankaur1137
      @harkirankaur1137 2 года назад

      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.

    • @harkirankaur1137
      @harkirankaur1137 2 года назад

      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

  • @Chandrashekhar-vp2yq
    @Chandrashekhar-vp2yq 3 года назад

    Not working on Gunicorn, heroku

    • @jessejulian9069
      @jessejulian9069 3 года назад

      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?

  • @stepup8108
    @stepup8108 3 года назад

    eventloop vs parallel thread which one is good ?

    • @SoumilShah
      @SoumilShah  3 года назад

      At end of day they do same thing )

    • @stepup8108
      @stepup8108 3 года назад

      @@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

    • @SoumilShah
      @SoumilShah  3 года назад

      @@stepup8108 yes or you fledge the work through Api on python language if you know what I am talking about

    • @stepup8108
      @stepup8108 3 года назад

      @@SoumilShah got it , but my background is javascript so more inclined to nodejs rather than python

    • @SoumilShah
      @SoumilShah  3 года назад

      @@stepup8108 you can always fan out worker on aws lambda to write your code on lambda call the lambda from node )