piping postgres COPY in python with psycopg2

后端 未结 2 1232
不知归路
不知归路 2021-02-06 06:55

I\'m writing a script to do a copy of some data between two machines on the same network using psycopg2. I\'m replacing some old, ugly bash that does the copy with



        
2条回答
  •  甜味超标
    2021-02-06 07:11

    You could use a deque that you've subclassed to support reading and writing:

    from collections import deque
    from Exceptions import IndexError
    
    class DequeBuffer(deque):
        def write(self, data):
            self.append(data)
        def read(self):
            try:
                return self.popleft()
            except IndexError:
                return ''
    
    buf = DequeBuffer()
    

    If the reader is much faster than the writer, and the table is large, the deque will still get big, but it will be smaller than storing the whole thing.

    Also, I don't know for sure return '' when the deque is empty is safe, rather than retrying until it's not empty, but I'd guess it is. Let me know if it works.

    Remember to del buf when you're sure the copy is done, especially if the script isn't just exiting at that point.

提交回复
热议问题