Forums

Threadin problem in Flaskapp

Hi

flask_app.py

from flask import Flask
from flask import render_template
import sched, time,thread
import tweet

sc = sched.scheduler(time.time, time.sleep)

app = Flask(__name__)

@app.route('/')
def hello_world():
    return render_template('index.html')

@app.route('/tweeter')
def tweeter():
    def do_something():
              tweet.tweetIt()
              sc.enter(20, 1, do_something,())
              sc.run()

    thread.start_new_thread(do_something,())
    return "tweeted everything"

The above code is my flask_app.py. As you can see I am using threading in the code.The code works just fine in localhost.But I am having trouble With that thread on your server.I think the problem may be because of your uWSGi protocol or the server,that is serving our flask app.Please let me know the errors in the app as soon as you can.

Hello:

You don't need to use threading to achieve your goal (which, is I suspect, a consequence of you wanting to effectively have more than one web worker).

Can I suggest you separate out the tweet logic into a separate process, that doesn't sit on the PythonAnywhere webservers (use a console server or equivalent instead). Have the webserver drop a file into a directory, and then have your console app read the file, delete it, and then tweet.

In other words, use a directory as a super simple queue.

Thanks,

Robert

Hi Thanks. I dont want this as a console application.The above code is working in my local host but not on this server. I want to run this thread for ever,and tweet for me with data given from an api. And also want all the other flask views to be accessible. I suspect that the problem may have raised because,we are not running the flask app by calling app.run().

I found somthing >> here. << But dont know how to apply that in my case.

I don't work for PythonAnywhere, so don't take my words as gospel, but:

I assume that PythonAnywhere consciously disables threading on their webservers. The idea is that Flask, and their webservers handle parallel processing for you. So, in a paid plan (with multiple web workers), you don't need threading, because a new web worker will handle a Flask app in parallel while your first web worker was doing the tweeting.

In other words, if you want to do things in parallel, you need to pay up for a premium account. (Or, hack around it by using a console app.)

Okay Thank you very much. I will try to Host the same code in heroku.I dont want to run it as a console app. I dont know why :)

Regards Rohit

(Personally, I like running things in the console, because then I can print intelligent status messages, and look at the console to see if things aren't working as they should be. But then I'm strange like that.)

hmm.. I do the same locally :)

It would be great if we can run the flaskapp on the port 80 as a console application.

It's unlikely that will ever be supported on PythonAnywhere. Our web servers and our console servers are optimised differently.