0

My python flask app runs using nohup. ie it is always live. I see that it creates a thread every time user submits from page. It is because flask.run is with multithread=true. But my problem is even after the processing is over, the thread doesn't seem to be closed. I'm checking this with the ps -eLf |grep userid command. where i see many threads still active long after the code execution is over. and it gets added when another submit is done. All threads are removed when the app itself is restarted.

What is the criteria for the thread to close without restarting the app?

Many posts like these suggests the gc.collect, del object etc.. I have many user defined classes getting instantiated on submit. and one object refers another . So

is it because the memory not getting released?

Should i use gc.collect or del objects? Pythons should be clearing these objects once the scope of the variable is over. is it correct?

app = Flask(__name__)
@app.route('/submit',methods = ['GET','POST'])
def submit():
    #obj1=class1()
    #obj2=class2(obj1)
    #obj3=class3(obj1)
    #refer objects
    #process data
    #done

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=4000, threaded=True, debug=False)
sjd
  • 1,329
  • 4
  • 28
  • 48
  • 1
    Flask's built-in development server really isn't intended for multithreaded use, hence the lack of support for thread management. I'd suggest reading into the pros and cons of the recommended deployment servers and see which suits your needs best. Popular choices: Gunicorn, Twisted, SocketIO, Gevent, nginx. – amitchone May 18 '18 at 13:43
  • this is also comparatively a small web app . but few users uses simultaneousely. so didnt look for a better platform than flask. I would definitely look at one of these above. – sjd May 21 '18 at 11:58

1 Answers1

0

It looks like the problem was with a paramiko object not getting closed. Once the SFTPClient or the SSHClient is opened, it has to be closed explicitly. I have assumed that along with my class object (where paramiko object is defined) it would get closed. But it doesnt.

So on the end of my process i call below lines. Now the threads seems getting closed properly

    if objs.ssh:
        objs.ssh.close()
    if objs.sftp:
            objs.t.close()
            objs.sftp.close()
    del objs
    gc.collect()
sjd
  • 1,329
  • 4
  • 28
  • 48