与我发布的另一篇文章类似,这回复了帖子并创建了一个新问题.
回顾:我需要更新空间数据库中的每条记录,其中我有一个覆盖多边形数据集的点数据集.对于每个点要素,我想指定一个键,使其与其所在的面要素相关联.因此,如果我的观点'纽约市'位于多边形美国,而美国多边形'GID = 1',我将为我的点纽约市分配'gid_fkey = 1'.
好的,所以这已经通过多处理实现了.我注意到使用它的速度提高了150%所以它确实有效.但我认为有一堆不必要的开销,因为每条记录需要一个数据库连接.
所以这是代码:
import multiprocessing, time, psycopg2
class Consumer(multiprocessing.Process):
def __init__(self, task_queue, result_queue):
multiprocessing.Process.__init__(self)
self.task_queue = task_queue
self.result_queue = result_queue
def run(self):
proc_name = self.name
while True:
next_task = self.task_queue.get()
if next_task is None:
print 'Tasks Complete'
self.task_queue.task_done()
break
answer = next_task()
self.task_queue.task_done()
self.result_queue.put(answer)
return
class Task(object):
def __init__(self, a):
self.a = a
def __call__(self):
pyConn = psycopg2.connect("dbname='geobase_1' host = 'localhost'")
pyConn.set_isolation_level(0)
pyCursor1 = pyConn.cursor()
procQuery = 'UPDATE city SET gid_fkey = gid FROM country …Run Code Online (Sandbox Code Playgroud) 我需要更新空间数据库中的每条记录,其中我有一个覆盖多边形数据集的点数据集。对于每个点要素,我想分配一个键以将其与它所在的多边形要素相关联。因此,如果我的点“纽约市”位于多边形美国内,而对于美国多边形“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)
Run Code Online (Sandbox Code Playgroud)
目前,我正在从另一个查询中获取 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 …Run Code Online (Sandbox Code Playgroud)