pg.ddx.io  pgsql-odbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Michal Vitecek <fuf@mageo.cz>
To: pgsql-odbc@postgresql.org
Subject: python, threads and pgsqlodbc problems
Date: Fri, 7 Sep 2007 11:27:56 +0200
Message-ID: <20070907092756.GA27902@mageo.cz> (raw)

 hello everyone,

 recently one of my projects which uses threads extensively started to
 freeze. after some digging i found out that the freezes are caused by
 2+ threads doing UPDATE to the same row in the same table:

   thread 1                    |                    thread 2
   ----------------------------+----------------------------
   BEGIN TRANSACTION           | BEGIN TRANSACTION
   ----------------------------+----------------------------
   UPDATE tableA SET           |
   columnA = 1 WHERE           |
   columnB = 2                 |
   ----------------------------+----------------------------
                               | UPDATE tableA SET
                               | columnA = 1 WHERE
                               | colunmB = 2
   ----------------------------+----------------------------
   COMMIT TRANSACTION (*)      | COMMIT TRANSACTION (*)

 (*) never reached

 the simplest code that exhibits the problem is:

---CUT HERE---
#!/usr/bin/python
import threading
import time
import mx.ODBC.unixODBC as ODBC

class Worker(threading.Thread):
    def __init__(self, sleepBeforeUpdateTime, sleepAfterUpdateTime):
        threading.Thread.__init__(self)
        self.sleepBeforeUpdateTime = sleepBeforeUpdateTime
        self.sleepAfterUpdateTime = sleepAfterUpdateTime

    def run(self):
        d = ODBC.connect(DSN, UID, PWD)
        c = d.cursor()
        c.execute("BEGIN TRANSACTION")
        time.sleep(self.sleepBeforeUpdateTime)
        c.execute("UPDATE tableA SET columnA = 1 WHERE columnB = 2")
        time.sleep(self.sleepAfterUpdateTime)
        c.execute("COMMIT TRANSACTION")
        print "never reached"    # this line is NEVER reached
        d.close()

workers = (
    Worker(0.0, 1.0),            # sleep after issuing UPDATE
    Worker(0.4, 0.0),            # sleep a while before issuing UPDATE
)
for worker in workers:
    worker.start()

time.sleep(5)
print "done"
---CUT HERE---

 i'm using python 2.4.4., mxODBC 2.0.7, unixODBC 2.2.12, psqlodbc
 08.02.0400 and postgresql 8.2.4.
 
 when two processes are doing the same all is working correctly. by
 tracing the process i've found out that the second UPDATE is issued but
 the call to SOCK_get_id() in connection.c doesn't return because the
 database waits for the 1st UPDATE to be either committed or rolled
 back. but since the 1st worker is blocked by the 2nd one it's never
 given chance to issue COMMIT and thus there's no way to recover from
 the situation.

 is there any easy way to remedy the problem?
 
 p.s.: changing the project to use processes instead of threads is a way
 to go, but if there was a simpler solution, i'd be happy to take that
 path.


        thanks a lot,
                    Michal
-- 
		fuf		(fuf@mageo.cz)



view thread (2+ messages)  latest in thread

Message-ID: <20070907092756.GA27902@mageo.cz>
Permalink:  ../20070907092756.GA27902@mageo.cz/
Also on:    postgresql.org/message-id/20070907092756.GA27902@mageo.cz

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-odbc@postgresql.org
  Cc: fuf@mageo.cz
  Subject: Re: python, threads and pgsqlodbc problems
  In-Reply-To: <20070907092756.GA27902@mageo.cz>
  References: <20070907092756.GA27902@mageo.cz>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox