2009-07-18 14 views
1

にアクセスしたときにクラッシュすると、正常に動作し、簡単なスレッドプログラムです:Pythonのスレッド - 彼らはここpostgreSQLの

import psycopg2 
import threading 
import time 

class testit(threading.Thread): 
    def __init__(self, currency): 
     threading.Thread.__init__(self) 
     self.currency = currency 

    def run(self): 
     global SQLConnection 
     global cursor 
     SQLString = "Select dval from ddata where dname ='%s' and ddate = '2009-07-17'" \ 
       %self.currency 
     z = time.time() 
     while (time.time() - z) < 2: 
      print SQLString 

SQLConnection = psycopg2.connect(database = "db", user = "xxxx", password = "xxxx") 
cursor = SQLConnection.cursor() 

a = testit('EURCZK') 
b = testit('EURPLN') 
a.start() 
b.start() 

しかし、すぐに私は次のコードでスレッドでのPostgreSQLデータベースにアクセスして起動しようとして、私はいつも

import psycopg2 
import threading 
import time 

class testit(threading.Thread): 
    def __init__(self, currency): 
     threading.Thread.__init__(self) 
     self.currency = currency 

    def run(self): 
     global SQLConnection 
     global cursor 
     SQLString = "Select dval from ddata where dname ='%s'and ddate = '2009-07-17'" %self.currency 
     z = time.time() 
     while (time.time() - z) < 2: 
      cursor.execute(SQLString) 
      print cursor.fetchall() 

SQLConnection = psycopg2.connect(database = "db", user = "xxxx", password = "xxxx") 
cursor = SQLConnection.cursor() 

a = testit('EURCZK') 
b = testit('EURPLN') 
a.start() 
b.start() 

唯一の違いはwhileループです。私はスレッドプログラミングにかなり新しいです。 postgresライブラリ(psycopg2)は "スレッドセーフ"ではありませんか?これはすべてWindows XP上で実行されています。私は何でもできる?

ありがとうございました。

+0

psycopgこれを行うことができます。正確なエラーメッセージは何ですか? – Christopher

+0

エラーメッセージが表示されません。クリストファー - 大きなウィンドウが表示され、メモリの一部の場所を読み取ることができません。したがって、適切なシステムレベルのクラッシュが発生します。私は各スレッドに独自の接続とカーソルを与えようとします。 –

+0

ハァッ。はい。まあ、http://www.free-soft.org/FSM/english/issue01/fog.htmlは、元の試行が有効だったことを示しています。おそらくこれはv2で変更されました。 – Christopher

答えて

2
global SQLConnection 
global cursor 

複数のスレッドからグローバルにアクセスしているようですか? 決してする必要があります。それらのグローバルがスレッドセーフでない場合、または適切なロックを自分で提供している場合を除きます。

これで、2つのスレッドが同じ接続と同じカーソルにアクセスするようになりました。彼らはお互いのつま先に足を踏み入れます。 psycopg2接続はスレッドセーフであるかもしれませんが、カーソルはありません。

スレッドごとに1つのカーソル(恐らく1つの接続も同様)を使用します。

+0

gotcha nos。あなたが答えている間、私は自分のやり方の誤りを考え出しました。私自身の答えを見てください。どうもありがとう。 –

0

ビンゴが働いています。誰かが答えを残したが、それを取り除いたように見え、各スレッドに独自の接続を与える。そしてそれはそれを解決するうん。したがって、このコードの動作:

import psycopg2 
import threading 
import time 

class testit(threading.Thread): 
    def __init__(self, currency): 
     threading.Thread.__init__(self) 
     self.currency = currency 
     self.SQLConnection = psycopg2.connect(database = "db", user = "xxxx", password = "xxxx") 
     self.cursor = self.SQLConnection.cursor() 

    def run(self): 
     SQLString = "Select dval from ddata where dname ='%s' and ddate = '2009-07-17'" \ 
       %self.currency 
     z = time.time() 
     while (time.time() - z) < 2: 
      self.cursor.execute(SQLString) 
      print self.cursor.fetchall() 

a = testit('EURCZK') 
b = testit('EURPLN') 
a.start() 
b.start() 
+0

私は元の回答を残しましたが、あなたがしようとしていたことができるはずだったはずのpsycopgの文書を読むと削除されました。 :-) – Christopher

+0

接続を保護する必要はありません。 'psycopg2.threadsafety'の値とそれが何を意味するのかを見てください(http://www.python.org/dev/peps/pep-0249/)。 –

+0

この例:http://initd.org/svn/psycopg/psycopg2/trunk/examples/threads.pyは、あなたが試していたことをすることができるはずであることを示しています。とにかく、すべてのスレッドに対して1つの接続を行うことができない場合でも、接続プールを試してみてください。 – Christopher

関連する問題