【发布时间】:2010-11-12 00:43:50
【问题描述】:
这是一个运行良好的简单线程程序:
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 - 只有一个大窗口停止标志它无法读取某些内存位置。因此,适当的系统级崩溃。我将尝试为每个线程提供自己的连接和光标。
-
嗯。好的。好吧,free-soft.org/FSM/english/issue01/fog.html 表明您最初的尝试是有效的。也许这在 v2 中有所改变。
标签: python postgresql