【发布时间】:2011-09-20 14:32:53
【问题描述】:
我需要更新空间数据库中的每条记录,其中我有一个点数据集覆盖多边形数据集。对于每个点要素,我想分配一个键以将其与它所在的多边形要素相关联。因此,如果我的点“纽约市”位于多边形 USA 内,并且对于美国多边形“GID = 1”,我将为我的点纽约市分配“gid_fkey = 1”。
为此,我创建了以下查询。
procQuery = 'UPDATE city SET gid_fkey = gid FROM country WHERE ST_within((SELECT the_geom FROM city WHERE wp_id = %s), country.the_geom) AND city_id = %s' % (cityID, cityID)
目前,我从另一个查询中获取 cityID 信息,该查询只选择 gid_fkey 为 NULL 的所有 cityID。本质上,我只需要遍历这些并运行前面显示的查询。由于查询仅依赖于另一个表中的静态信息,理论上所有这些过程都可以一次运行。我已经实现了下面的线程过程,但我似乎无法迁移到多处理
import psycopg2, pprint, threading, time, Queue
queue = Queue.Queue()
pyConn = psycopg2.connect("dbname='geobase_1' host='localhost'")
pyConn.set_isolation_level(0)
pyCursor1 = pyConn.cursor()
getGID = 'SELECT cityID FROM city'
pyCursor1.execute(getGID)
gidList = pyCursor1.fetchall()
class threadClass(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
def run(self):
while True:
gid = self.queue.get()
procQuery = 'UPDATE city SET gid_fkey = gid FROM country WHERE ST_within((SELECT the_geom FROM city WHERE wp_id = %s), country.the_geom) AND city_id = %s' % (cityID, cityID)
pyCursor2 = pyConn.cursor()
pyCursor2.execute(procQuery)
print gid[0]
print 'Done'
def main():
for i in range(4):
t = threadClass(queue)
t.setDaemon(True)
t.start()
for gid in gidList:
queue.put(gid)
queue.join()
main()
我什至不确定多线程是否是最佳的,但它肯定比一个接一个地更快。
我将使用的机器有四个内核(四核)和一个没有 GUI、PostgreSQL、PostGIS 和 Python 的最小 Linux 操作系统,如果这会有所不同的话。
我需要进行哪些更改才能启用这个极其简单的多处理任务?
【问题讨论】:
标签: python postgresql multiprocessing postgis