Flask does not expect function return to show view

Asked

Viewed 30 times

0

I have a function that returns values to be shown in the view. In tests on localhost works perfectly, the server waits for the return of this function and only then presents the data on the screen.

The problem is that when I deploy to Heroku, the server does not expect this return from the function and shows the empty return. I have tried with several options, threading, Concurrent., all work locally, but do not work when I deploy.

with concurrent.futures.ThreadPoolExecutor() as e:
        future = e.submit(get_movie_players, soup)
        links_list = future.result()

        movie_data.append({'name':movie_name,
        'cover': movie_cover[0].replace(');',''),
        'thumbnail': movie_thumb,
        'sinopsys': sinopys,
        'date': movie_date,
        'duration': movie_duration,
        'links': links_list})

In the example, the variable links_list in localhost has the result of Future, but it’s empty when I deploy.

1 answer

1

since you have to wait for the function to return there, the simplest is __not usar_ Concurrent.Futures, nor threads - just do links_list = get_movie_players(soup).

Using these parallel call strategies is just useful when you don’t need to wait for a function to return.

You haven’t absolutely none won with that drawing. As it is a web application, what will take care of the parallelism for you, so the application can respond to more than one pageview at a time is the wsgi server - in a manual configuration you choose uwsgi, gunicorn, or some other. Heroku uses his own.

Anyway, the call to result() without passing a timeout, should expect parallel execution - what happens is that you should be bumping into process limits of your Heroku account - do not remember their terms of service, but it is very likely that the free account limits processes, and maybe even threads, that you can fire - who controls threads and processes is the wsgi server (which in this case is under Heroku’s control, not yours).

If you really need to parallelize the calls there - (that is, a single ageview will show the result of multi-page Scrapping), you will need to redesign the entire application - to return a "skeleton" html, and pass the asynchronous results to the web page using some asynchronous technique (ajax calls, React development, etc)..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.