這裏是一個簡單的線程程序的正常工作: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上運行。我能做什麼?
謝謝。
psycopg可以做到這一點。什麼是你正在得到確切的錯誤信息? – Christopher 2009-07-18 22:00:15
沒有錯誤信息克里斯托弗 - 只有一個大窗口停止標記,它無法讀取一些內存位置。所以適當的系統級別崩潰。我將嘗試爲每個線程提供自己的連接和遊標。 – 2009-07-18 22:02:20
呵呵。好的。那麼,http://www.free-soft.org/FSM/english/issue01/fog.html表明您的原始嘗試是有效的。也許這在v2中已經改變了。 – Christopher 2009-07-18 22:13:47