Code Monkey home page Code Monkey logo

Comments (10)

danblack avatar danblack commented on June 26, 2024

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.

jwp avatar jwp commented on June 26, 2024

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.

danblack avatar danblack commented on June 26, 2024

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.

jwp avatar jwp commented on June 26, 2024

Yeah, connection per thread should just work.

from fe.

seidlmic avatar seidlmic commented on June 26, 2024

Hello, I would like to ask if it is still true that to establish and close connection inside threads is OK?

from fe.

jwp avatar jwp commented on June 26, 2024

Should be. However, when closing a Connection, only one thread should be performing the operation.

from fe.

seidlmic avatar seidlmic commented on June 26, 2024

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.

jwp avatar jwp commented on June 26, 2024

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.

seidlmic avatar seidlmic commented on June 26, 2024

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.

jwp avatar jwp commented on June 26, 2024

Use the issue as documentation.

from fe.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.