使用Python(PostGIS/PostgreSQL)进行令人尴尬的并行数据库更新

2024-06-26 17:40:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要更新空间数据库中的每个记录,其中我有一组数据点,覆盖了多边形的数据集。对于每个点要素,我要指定一个关键点,使其与它所在的多边形要素相关联。因此,如果我的点“纽约市”位于多边形美国境内,而对于美国多边形“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为空的所有cityID。实际上,我只需要遍历这些并运行前面显示的查询。由于查询理论上只依赖于另一个表中的静态信息,所以所有这些进程都可以同时运行。我已经实现了下面的线程过程,但我似乎不能迁移到多处理

^{pr2}$

我甚至不确定多线程是否是最佳的,但它绝对比逐个执行要快。在

我将要使用的机器有四个内核(四核)和一个最小的Linux操作系统,没有GUI、PostgreSQL、PostGIS和Python,如果这有区别的话。在

我需要做些什么来启用这个非常简单的多处理任务?在


Tags: the数据from信息idcitywhere多边形
2条回答

在纯SQL中,可以执行以下操作:

UPDATE city ci
SET gid_fkey = co.gid 
FROM country co 
WHERE ST_within(ci.the_geom , co.the_geom) 
AND ci.city_id = _some_parameter_
        ;

如果一个城市适合多个国家(导致对同一目标行进行多次更新),可能会出现问题,但在您的数据中可能不是这样。在

好吧,这是对我自己帖子的回复。干得好我=D

使我的系统从单核线程到四核多处理的速度提高了大约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  WHERE ST_within((SELECT the_geom FROM city WHERE city_id = %s), country.the_geom) AND city_id = %s' % (self.a, self.a)

    pyCursor1.execute(procQuery)
    print 'What is self?'
    print self.a

    return self.a

def __str__(self):
    return 'ARC'
def run(self):
    print 'IN'

if __name__ == '__main__':
tasks = multiprocessing.JoinableQueue()
results = multiprocessing.Queue()

num_consumers = multiprocessing.cpu_count() * 2
consumers = [Consumer(tasks, results) for i in xrange(num_consumers)]
for w in consumers:
    w.start()

pyConnX = psycopg2.connect("dbname='geobase_1' host = 'localhost'")
pyConnX.set_isolation_level(0)
pyCursorX = pyConnX.cursor()

pyCursorX.execute('SELECT count(*) FROM cities WHERE gid_fkey IS NULL')    
temp = pyCursorX.fetchall()    
num_job = temp[0]
num_jobs = num_job[0]

pyCursorX.execute('SELECT city_id FROM city WHERE gid_fkey IS NULL')    
cityIdListTuple = pyCursorX.fetchall()    

cityIdList = []

for x in cityIdListTuple:
    cityIdList.append(x[0])


for i in xrange(num_jobs):
    tasks.put(Task(cityIdList[i - 1]))

for i in xrange(num_consumers):
    tasks.put(None)

while num_jobs:
    result = results.get()
    print result
    num_jobs -= 1

现在我有另一个问题,我已经贴在这里了:

Create DB connection and maintain on multiple processes (multiprocessing)

希望我们能摆脱一些开销,让这个孩子更快。在

相关问题 更多 >