Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working with Threads #38

Closed
jwp opened this issue Feb 27, 2010 · 13 comments
Closed

Working with Threads #38

jwp opened this issue Feb 27, 2010 · 13 comments
Labels
howto Issues that offer guidance for paticular tasks.

Comments

@jwp
Copy link
Contributor

jwp commented Feb 27, 2010

Add a section to the driver documentation about working with threads. Most drivers offer thread safety, but driver.pq3 does not.

Discuss: one connection per thread, synchronizing using a queue, and explicit locking.

@danblack
Copy link

danblack commented Dec 8, 2011

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"

@jwp
Copy link
Contributor Author

jwp commented Dec 8, 2011

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.

@danblack
Copy link

danblack commented Dec 8, 2011

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" :)

@jwp
Copy link
Contributor Author

jwp commented Dec 8, 2011

Yeah, connection per thread should just work.

@seidlmic
Copy link

seidlmic commented Feb 9, 2022

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

@jwp
Copy link
Contributor Author

jwp commented Feb 9, 2022

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

@seidlmic
Copy link

seidlmic commented Feb 9, 2022

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.

@jwp
Copy link
Contributor Author

jwp commented Feb 9, 2022

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.

@seidlmic
Copy link

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 :)

@jwp jwp added howto Issues that offer guidance for paticular tasks. and removed documentation labels Feb 9, 2023
@jwp
Copy link
Contributor Author

jwp commented Feb 9, 2023

Use the issue as documentation.

@jwp jwp closed this as completed Feb 9, 2023
@seidlmic
Copy link

Hello, after one year with happy usage of model "connection per thread" new error sometimes rises. It looks like it always happens at the very beginning when the multiple threads start at the same time. The error looks like this:

  File "/usr/local/gabina/venv/m_backupSchema-2.1.2/lib/python3.11/site-packages/postgresql/__init__.py", line 74, in open
    std_params = _pg_param.collect(prompt_title = None)
                 ^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'collect'

Can it be some race condition with importing pypostgresql library? Should be imported in main thread or child thread?

https://github.com/python-postgres/fe/blob/1cd1b4476739fbcb0dc840fe5a2260209eba75c6/postgresql/__init__.py#L37C1-L37C40

@jwp
Copy link
Contributor Author

jwp commented Oct 27, 2023

_pg_param is the last global initialized and _pg_iri is the first, so I think so. Naturally, this will only occur if you're using postgresql.open. Using Connectors directly instead of postgresql.open is advisable for most applications.

Workarounds:

  • Perform one postgresql.open prior to launching any threads.
  • Fill in the globals manually(_pg_param, _pg_iri, _pg_driver).
  • Patch init.py to check all globals (planned fix for this)

The deferred imports, likely, don't have any value these days. They were used to avoid unused modules in applications that used Connectors directly. Today, the primary advantage this has is dodging bitrot in _pg_param.

The third workaround is what I plan to apply. Remaining synchronization issues should be handled by Python's import locks and object level locks in nogil Pythons.

@seidlmic
Copy link

Thanks for advice we have changed open() with driver.connect() and problem is gone :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
howto Issues that offer guidance for paticular tasks.
Projects
None yet
Development

No branches or pull requests

3 participants