Forums

Run a script on console vs Flask script

Hello, I have a website on pythonanywhere with Flask framework. My object with it is to keep me informed about cryptocurrencies, so when I refresh the page IT brings me some info. But only when I refresh the page!

This is a problem when I am not looking. Thus I wrote a a script(another py file) to run forever with 1 min. intervals. This file gets the data that I mentioned above and stores it into DB or a text file which then I plan to see(with a graph)on my flask app, on a different report page. So my question is: Is it possible to run this always runing py file in my Flask app on pythonanywhere? And not affecting the web service at the same time or should I run it on bash console and then read and display the data with my report page.

I experienced that if console is idle for a long time, It is closed. This means that my file will stop running. But then again I don't know how to run a py file alongside with a running Flask app

Which way should I try and how ?

Thank you in advance

If you need a browser viewing your website to always show up-to-date information, and refreshing the page runs the appropriate code to get that information (without your other script) then perhaps the best way to do what you want would be to add some code to the page's HTML to refresh it periodically? This would refresh the page every minute:

<meta http-equiv="refresh" content="60" >

(I suggest doing it that way rather than somehow using your console script because the only way to keep a server-side script running constantly, if it's not a website's code, is to use an always-on task, and they're only available in paid plans.)

If I add meta tag you suggested, I would have to keep an open page all the time to insert the data into db or txt, is that right? This would not serve my purpose. When I lost the connection to the page data won't be added with new timestamp and line graph(or chart) will be null for that time frame.

But if somehow meta tag keeps the data added all the time even after the browser is closed, then I can try your suggestion.

Looking forward for your answer. Thank you

Yes, you would need to keep the page open to keep the updates happening. As giles said, if you want something to run all the time, you need to use an always on task in a paid account.