Comments (10)
Hello, James.
I am using driver.pq3 in my multithread application (connection per thread).
I found that lazy initilization of module's varaibles sometimes makes me to organize my source to prevent errors.
global _pg_iri, _pg_driver, _pg_param if _pg_iri is None: from . import iri as _pg_iri from . import driver as _pg_driver from . import clientparameters as _pg_param
But I think, I will be relatively easy to make support of thread safety for model "connection per thread"
from fe.
Hmm. Yeah, that was silly of me.
Could you see if this fixes your issue:
global _pg_iri, _pg_driver, _pg_param
if _pg_iri is None:
from . import driver as _pg_driver
from . import clientparameters as _pg_param
from . import iri as _pg_iri
Having _pg_iri last should make the race condition benign.
from fe.
Thank you I will try if it is necessary
And... when I wrote that "I will be relatively easy to make" I meant "It will be relatively easy to make" :)
from fe.
Yeah, connection per thread should just work.
from fe.
Hello, I would like to ask if it is still true that to establish and close connection inside threads is OK?
from fe.
Should be. However, when closing a Connection, only one thread should be performing the operation.
from fe.
Hm, not very clear to me. I have one main thread that creates and starts child threads in unpredictable time (all at ones, one after another, overlapping in some way). In each child thread I create separate db connection by calling postgresql.open()
and perform queries with db.query(), db.prepare()
. What do you mean "However, when closing a Connection, only one thread should be performing the operation"?
- I can not rely on Python garbage cleaning when exiting thread code?
- If I have to close db connection with
db.close()
, closing one connection can in some way interact with connections from other threads? I am afraid there is no way to do it as threads are completely time independent. They can start and end any time.
from fe.
I can not rely on Python garbage cleaning when exiting thread code?
If you rely on garbage collection, I believe a resource warning will be emitted by Python's socket module.
Doing so will only cause the socket to be closed and no termination message will be issued to the server.
I would not advise relying on garbage collection for Connection close.
If I have to close db connection with db.close(), closing one connection can in some way interact with connections from other threads? I am afraid there is no way to do it as threads are completely time independent. They can start and end any time.
No. The driver is not thread aware and there is no communication between connections. You can either db.close()
or with db:
, but only one thread should be performing the operation on a single Connection. Sharing a single Connection across multiple threads is not safe unless operations are protected by a synchronization primitive.
If each thread has its own Connection, you're safe.
from fe.
Thanks for explanation. Yes I establish and close connection explicitly in thread function (not passing to thread) so I will early find out if I am safe :)
from fe.
Use the issue as documentation.
from fe.
Related Issues (20)
- Connection fails on linux HOT 2
- Tag 1.2.1 HOT 1
- Unknown type taken as array HOT 2
- IPv6 literals in connection strings are not parsed correctly HOT 2
- Postgres version string from ubuntu still causing errors [BugFix] HOT 7
- Connection problem HOT 7
- How to get list instead of postgresql.types.Array HOT 5
- SASL SCRAM Support HOT 3
- Trap Version Parsing Errors (ValueError on connect) HOT 8
- Improve postgresql.api's quality
- Improve consistency with libpq URIs HOT 1
- More flexible parameterized statements HOT 4
- Connection seems to fail with a returned banner HOT 3
- Connector throwing AuthenticationMethodError HOT 1
- Cannot connect to Postgres 14.2 on Ubuntu HOT 1
- Collections has no attribute 'Iterator' HOT 9
- Deprecation updates for v1.3.1 HOT 4
- Add `secure` parameter to Connectors to simplify TLS. HOT 1
- Connection Pool Servers and DuplicateCursorError / DuplicatePreparedStatementError HOT 7
- Project Future
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fe.