Psycopg connection pool and threads not working properly











up vote
-2
down vote

favorite












I have a connection pool (ThreadedConnectionPool) and threads that when I'm executing I get a connection from the pool, does all it's stuff and then release connection by putconn(), but if I do this inside the thread it doesn't release properly the connection and gets "connection pool exhausted". I tryed doing this outside the thread and everything's fine. I passed the connection pool to the thread's executed function but it doesn't work anyway. Any idea? Thank you.



dbconn_pool = psycopg2.pool.ThreadedConnectionPool(minconn=1, maxconn=3,
host="XXX",
port="XXX",
dbname="XXX",
user="XXX",
password="XXX")

def processing_informations(conn_pool):
db_connection = conn_pool.getconn()
db_connection.set_session(readonly=True, autocommit=True)
cur = db_connection.cursor(cursor_factory=RealDictCursor)
.....more....
conn_pool.putconn(db_connection)

def start_processor():
new_thread = threading.Thread(target=self.processing_informations, args=[dbconn_pool])
new_thread.daemon = True
new_thread.start()

while true:
start_processor()
sleep(100)









share|improve this question




















  • 2




    You'd provide example code to clarify whot you are doing. Or even better a Minimal, Complete, and Verifiable example
    – Selaron
    Nov 15 at 12:08










  • i added a basic example, the project is not properly like this but i tried to expose the parte the is getting the issue
    – Giuseppe
    Nov 15 at 13:29















up vote
-2
down vote

favorite












I have a connection pool (ThreadedConnectionPool) and threads that when I'm executing I get a connection from the pool, does all it's stuff and then release connection by putconn(), but if I do this inside the thread it doesn't release properly the connection and gets "connection pool exhausted". I tryed doing this outside the thread and everything's fine. I passed the connection pool to the thread's executed function but it doesn't work anyway. Any idea? Thank you.



dbconn_pool = psycopg2.pool.ThreadedConnectionPool(minconn=1, maxconn=3,
host="XXX",
port="XXX",
dbname="XXX",
user="XXX",
password="XXX")

def processing_informations(conn_pool):
db_connection = conn_pool.getconn()
db_connection.set_session(readonly=True, autocommit=True)
cur = db_connection.cursor(cursor_factory=RealDictCursor)
.....more....
conn_pool.putconn(db_connection)

def start_processor():
new_thread = threading.Thread(target=self.processing_informations, args=[dbconn_pool])
new_thread.daemon = True
new_thread.start()

while true:
start_processor()
sleep(100)









share|improve this question




















  • 2




    You'd provide example code to clarify whot you are doing. Or even better a Minimal, Complete, and Verifiable example
    – Selaron
    Nov 15 at 12:08










  • i added a basic example, the project is not properly like this but i tried to expose the parte the is getting the issue
    – Giuseppe
    Nov 15 at 13:29













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











I have a connection pool (ThreadedConnectionPool) and threads that when I'm executing I get a connection from the pool, does all it's stuff and then release connection by putconn(), but if I do this inside the thread it doesn't release properly the connection and gets "connection pool exhausted". I tryed doing this outside the thread and everything's fine. I passed the connection pool to the thread's executed function but it doesn't work anyway. Any idea? Thank you.



dbconn_pool = psycopg2.pool.ThreadedConnectionPool(minconn=1, maxconn=3,
host="XXX",
port="XXX",
dbname="XXX",
user="XXX",
password="XXX")

def processing_informations(conn_pool):
db_connection = conn_pool.getconn()
db_connection.set_session(readonly=True, autocommit=True)
cur = db_connection.cursor(cursor_factory=RealDictCursor)
.....more....
conn_pool.putconn(db_connection)

def start_processor():
new_thread = threading.Thread(target=self.processing_informations, args=[dbconn_pool])
new_thread.daemon = True
new_thread.start()

while true:
start_processor()
sleep(100)









share|improve this question















I have a connection pool (ThreadedConnectionPool) and threads that when I'm executing I get a connection from the pool, does all it's stuff and then release connection by putconn(), but if I do this inside the thread it doesn't release properly the connection and gets "connection pool exhausted". I tryed doing this outside the thread and everything's fine. I passed the connection pool to the thread's executed function but it doesn't work anyway. Any idea? Thank you.



dbconn_pool = psycopg2.pool.ThreadedConnectionPool(minconn=1, maxconn=3,
host="XXX",
port="XXX",
dbname="XXX",
user="XXX",
password="XXX")

def processing_informations(conn_pool):
db_connection = conn_pool.getconn()
db_connection.set_session(readonly=True, autocommit=True)
cur = db_connection.cursor(cursor_factory=RealDictCursor)
.....more....
conn_pool.putconn(db_connection)

def start_processor():
new_thread = threading.Thread(target=self.processing_informations, args=[dbconn_pool])
new_thread.daemon = True
new_thread.start()

while true:
start_processor()
sleep(100)






python multithreading connection-pooling psycopg2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 at 13:28

























asked Nov 15 at 11:57









Giuseppe

6912




6912








  • 2




    You'd provide example code to clarify whot you are doing. Or even better a Minimal, Complete, and Verifiable example
    – Selaron
    Nov 15 at 12:08










  • i added a basic example, the project is not properly like this but i tried to expose the parte the is getting the issue
    – Giuseppe
    Nov 15 at 13:29














  • 2




    You'd provide example code to clarify whot you are doing. Or even better a Minimal, Complete, and Verifiable example
    – Selaron
    Nov 15 at 12:08










  • i added a basic example, the project is not properly like this but i tried to expose the parte the is getting the issue
    – Giuseppe
    Nov 15 at 13:29








2




2




You'd provide example code to clarify whot you are doing. Or even better a Minimal, Complete, and Verifiable example
– Selaron
Nov 15 at 12:08




You'd provide example code to clarify whot you are doing. Or even better a Minimal, Complete, and Verifiable example
– Selaron
Nov 15 at 12:08












i added a basic example, the project is not properly like this but i tried to expose the parte the is getting the issue
– Giuseppe
Nov 15 at 13:29




i added a basic example, the project is not properly like this but i tried to expose the parte the is getting the issue
– Giuseppe
Nov 15 at 13:29

















active

oldest

votes











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',
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%2f53318999%2fpsycopg-connection-pool-and-threads-not-working-properly%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53318999%2fpsycopg-connection-pool-and-threads-not-working-properly%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

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?