Create DB connection and maintain on multiple processes (multiprocessing)

前端 未结 1 1219
名媛妹妹
名媛妹妹 2020-12-12 19:51

Similar to another post I made, this answers that post and creates a new question.

Recap: I need to update every record in a spatial database in which I have a data

相关标签:
1条回答
  • 2020-12-12 20:10

    Try to isolate the creation of your connection in the Consumer constructor, then give it to the executed Task :

    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
            self.pyConn = psycopg2.connect("dbname='geobase_1' host = 'localhost'")
            self.pyConn.set_isolation_level(0)
    
    
        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(connection=self.pyConn)
                self.task_queue.task_done()
                self.result_queue.put(answer)
            return
    
    
    class Task(object):
        def __init__(self, a):
            self.a = a
    
        def __call__(self, connection=None):        
            pyConn = connection
            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'
    
    0 讨论(0)
提交回复
热议问题