creating flask webapp with sql alchemy












0















I'm building a small webapp using flask and hosted on PythonAnywhere. The app is made of an Exchange class where several Team classes interact. Each user who logins is related to a specific Team. The app will have no more than 30 connected users at the same time although they will have to get info in real time. There is also an always-on task which will also need to connect to the database.



I'm using sqlalchemy to manage the database connections but I'm constantly getting either (2006, 'MySQL server has gone away') or (2013, 'Lost connection to MySQL server during query'). Therefore I was wondering how I should manage concurrent connections within the app. I tried several configurations such as opening and closing the connections after every call in the flask app or even within every class method but nothing seems to work.



Should I make calls the database only from the flask app using for instance flask-sqlalchemy or are there other ways to achieve this?










share|improve this question

























  • So are you using flask-sqlalchemy right now? Because if you aren't, using it will probably be the solution.

    – Joost
    Nov 26 '18 at 13:32











  • Do you mean like moving all database calls from the classes to the web app and use flask-sqlalchemy?

    – therealcode
    Nov 26 '18 at 14:05











  • It doesn't matter where you make calls to the database, it matters that you make calls using a Flask-SQLalchemy db. You only need to keep this in mind when initing your database, and if your database is init-ed as Flask-SQLAlchemy db, it will handle all kinds of funky errors and behaviour from there. Read here

    – Joost
    Nov 26 '18 at 14:15
















0















I'm building a small webapp using flask and hosted on PythonAnywhere. The app is made of an Exchange class where several Team classes interact. Each user who logins is related to a specific Team. The app will have no more than 30 connected users at the same time although they will have to get info in real time. There is also an always-on task which will also need to connect to the database.



I'm using sqlalchemy to manage the database connections but I'm constantly getting either (2006, 'MySQL server has gone away') or (2013, 'Lost connection to MySQL server during query'). Therefore I was wondering how I should manage concurrent connections within the app. I tried several configurations such as opening and closing the connections after every call in the flask app or even within every class method but nothing seems to work.



Should I make calls the database only from the flask app using for instance flask-sqlalchemy or are there other ways to achieve this?










share|improve this question

























  • So are you using flask-sqlalchemy right now? Because if you aren't, using it will probably be the solution.

    – Joost
    Nov 26 '18 at 13:32











  • Do you mean like moving all database calls from the classes to the web app and use flask-sqlalchemy?

    – therealcode
    Nov 26 '18 at 14:05











  • It doesn't matter where you make calls to the database, it matters that you make calls using a Flask-SQLalchemy db. You only need to keep this in mind when initing your database, and if your database is init-ed as Flask-SQLAlchemy db, it will handle all kinds of funky errors and behaviour from there. Read here

    – Joost
    Nov 26 '18 at 14:15














0












0








0








I'm building a small webapp using flask and hosted on PythonAnywhere. The app is made of an Exchange class where several Team classes interact. Each user who logins is related to a specific Team. The app will have no more than 30 connected users at the same time although they will have to get info in real time. There is also an always-on task which will also need to connect to the database.



I'm using sqlalchemy to manage the database connections but I'm constantly getting either (2006, 'MySQL server has gone away') or (2013, 'Lost connection to MySQL server during query'). Therefore I was wondering how I should manage concurrent connections within the app. I tried several configurations such as opening and closing the connections after every call in the flask app or even within every class method but nothing seems to work.



Should I make calls the database only from the flask app using for instance flask-sqlalchemy or are there other ways to achieve this?










share|improve this question
















I'm building a small webapp using flask and hosted on PythonAnywhere. The app is made of an Exchange class where several Team classes interact. Each user who logins is related to a specific Team. The app will have no more than 30 connected users at the same time although they will have to get info in real time. There is also an always-on task which will also need to connect to the database.



I'm using sqlalchemy to manage the database connections but I'm constantly getting either (2006, 'MySQL server has gone away') or (2013, 'Lost connection to MySQL server during query'). Therefore I was wondering how I should manage concurrent connections within the app. I tried several configurations such as opening and closing the connections after every call in the flask app or even within every class method but nothing seems to work.



Should I make calls the database only from the flask app using for instance flask-sqlalchemy or are there other ways to achieve this?







python flask sqlalchemy flask-sqlalchemy pythonanywhere






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 7:22









petezurich

3,61081734




3,61081734










asked Nov 26 '18 at 7:15









therealcodetherealcode

748




748













  • So are you using flask-sqlalchemy right now? Because if you aren't, using it will probably be the solution.

    – Joost
    Nov 26 '18 at 13:32











  • Do you mean like moving all database calls from the classes to the web app and use flask-sqlalchemy?

    – therealcode
    Nov 26 '18 at 14:05











  • It doesn't matter where you make calls to the database, it matters that you make calls using a Flask-SQLalchemy db. You only need to keep this in mind when initing your database, and if your database is init-ed as Flask-SQLAlchemy db, it will handle all kinds of funky errors and behaviour from there. Read here

    – Joost
    Nov 26 '18 at 14:15



















  • So are you using flask-sqlalchemy right now? Because if you aren't, using it will probably be the solution.

    – Joost
    Nov 26 '18 at 13:32











  • Do you mean like moving all database calls from the classes to the web app and use flask-sqlalchemy?

    – therealcode
    Nov 26 '18 at 14:05











  • It doesn't matter where you make calls to the database, it matters that you make calls using a Flask-SQLalchemy db. You only need to keep this in mind when initing your database, and if your database is init-ed as Flask-SQLAlchemy db, it will handle all kinds of funky errors and behaviour from there. Read here

    – Joost
    Nov 26 '18 at 14:15

















So are you using flask-sqlalchemy right now? Because if you aren't, using it will probably be the solution.

– Joost
Nov 26 '18 at 13:32





So are you using flask-sqlalchemy right now? Because if you aren't, using it will probably be the solution.

– Joost
Nov 26 '18 at 13:32













Do you mean like moving all database calls from the classes to the web app and use flask-sqlalchemy?

– therealcode
Nov 26 '18 at 14:05





Do you mean like moving all database calls from the classes to the web app and use flask-sqlalchemy?

– therealcode
Nov 26 '18 at 14:05













It doesn't matter where you make calls to the database, it matters that you make calls using a Flask-SQLalchemy db. You only need to keep this in mind when initing your database, and if your database is init-ed as Flask-SQLAlchemy db, it will handle all kinds of funky errors and behaviour from there. Read here

– Joost
Nov 26 '18 at 14:15





It doesn't matter where you make calls to the database, it matters that you make calls using a Flask-SQLalchemy db. You only need to keep this in mind when initing your database, and if your database is init-ed as Flask-SQLAlchemy db, it will handle all kinds of funky errors and behaviour from there. Read here

– Joost
Nov 26 '18 at 14:15












1 Answer
1






active

oldest

votes


















1














Your connections are timing out. Tell sqlalchemy to recycle connections after a time that is shorter than the timeout. Like this:



engine = create_engine('mysql+mysqldb://...', pool_recycle=280)





share|improve this answer
























  • I did this already although I put 299

    – therealcode
    Nov 26 '18 at 14:02











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53476271%2fcreating-flask-webapp-with-sql-alchemy%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Your connections are timing out. Tell sqlalchemy to recycle connections after a time that is shorter than the timeout. Like this:



engine = create_engine('mysql+mysqldb://...', pool_recycle=280)





share|improve this answer
























  • I did this already although I put 299

    – therealcode
    Nov 26 '18 at 14:02
















1














Your connections are timing out. Tell sqlalchemy to recycle connections after a time that is shorter than the timeout. Like this:



engine = create_engine('mysql+mysqldb://...', pool_recycle=280)





share|improve this answer
























  • I did this already although I put 299

    – therealcode
    Nov 26 '18 at 14:02














1












1








1







Your connections are timing out. Tell sqlalchemy to recycle connections after a time that is shorter than the timeout. Like this:



engine = create_engine('mysql+mysqldb://...', pool_recycle=280)





share|improve this answer













Your connections are timing out. Tell sqlalchemy to recycle connections after a time that is shorter than the timeout. Like this:



engine = create_engine('mysql+mysqldb://...', pool_recycle=280)






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 '18 at 12:52









GlennGlenn

4,71111321




4,71111321













  • I did this already although I put 299

    – therealcode
    Nov 26 '18 at 14:02



















  • I did this already although I put 299

    – therealcode
    Nov 26 '18 at 14:02

















I did this already although I put 299

– therealcode
Nov 26 '18 at 14:02





I did this already although I put 299

– therealcode
Nov 26 '18 at 14:02


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53476271%2fcreating-flask-webapp-with-sql-alchemy%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks