Forums

how to disable buffering?

Hi all,

Relatively new to Python, quite new to putting it on the web.

I'm working on a Flask project that (in part) takes some input from checkboxes, runs that through (what I believe you'd call) my main app page where I've also imported a function that works with the checkbox input, and spits the output from a function onto an HTML page in text format. I've gotten it to do that perfectly fine, except something somewhere in the process is caching the output in a way that makes it add onto itself. So, for example, if output #1 was "kitty" and output #2 was "puppy," I clicked output #1 and got the result and then refreshed it would resend and read "kitty kitty" or if I wen back and checked the box for output #2 and went forward it would read "kitty puppy," etc.

So, HOURS of looking things up, trying different things and having them fail has lead me to suspect this is a pythonanywhere-specific problem, as detailed here: https://help.pythonanywhere.com/pages/Buffering/

I tried throwing

headers['X-Accel-Buffering'] = 'no'

into my main my main app page in what seemed like the appropriate place, and then what seemed like a probably inappropriate place, and all it did was break the page where the output displays. Error log informs me that "name 'headers' is not defined."

Any more-detailed info on how to make pythonanywhere's buffering workaround work would be greatly appreciated. I assume there's something I need to import, and a specific place one needs to plug the pythonanywhere buffer workaround code above into one's app page, but I just don't seem to be able to figure that out. Help?

hmm. my instincts say buffering is not the issue here.

one way to check this is if you run your flask app locally and see if the inputs also add onto itself.

how are you doing the writing of the function on the html page? are you using the flask templating language? ie. jinja etc? if you are using a variable of some sort (say a variable called output), and you could print the variable before the page is rendered. print it to sys.stderr and you will see it in your error log. if here, you see that the the output is already adding onto itself, then there is something wrong with your code. For example, you could be appending instead of rewriting to a variable/file/database entry.

@Emily

I agree with conrad, and I fear you're following a red herring. Any chance of seeing snippets from your function(s) please?

Jim

I actually think you're both right- returning to my code, I think it's a list-building issue. I'm still fiddling with it, so I will return when I've fully figured it out and let you guys know if I was able to fix it or if I still suspect there's an additional buffering-related aspect to it.

Update: Yes, it was indeed an issue of me using lists carelessly.

I'd been so focused on learning the Flask framework and assuming I was still getting parts of that wrong that I didn't stop to think about what my code itself was actually doing to get onto the web page vs. what it was doing when I hit "save and run" in the file (which was not duplicating the problem in its output when I tested the function there). I.e. "save and run" reloads ALL of the code fresh, whereas (as I have it set up) I have a submit button that returns the result of a function that uses lists in a way that was also (and here's the part I wasn't thinking about) building them- but doesn't purge the whole program every time it's clicked. Returning the function once before printing it duplicated the problem in-file because it duplicated the behavior, which I was rather dense not to have tried first. Fixing how I was handling lists fixed the problem.

Thank you both, though- I would probably have stayed stuck chasing the wrong thing for a lot longer without the reality check. =)

We've all been there! And solving problems teaches you more than anything else.

+1 to that! I'm sure I've learned more from the debugging the typos I made when typing in code from tutorials than I ever did from the tutorials themselves ;-)