Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FetchMany MemoryError #25

Closed
tristanwietsma opened this issue Jul 1, 2014 · 3 comments
Closed

FetchMany MemoryError #25

tristanwietsma opened this issue Jul 1, 2014 · 3 comments

Comments

@tristanwietsma
Copy link

I'm using an iterator with fetchmany to stream a large result out as CSV:

class ODBCContext:

     def __init__(self, odbc_connection_string):
        self.conn_str = odbc_connection_string

    def __enter__(self):
        self.connection = pyodbc.connect(self.conn_str)
        self.cursor = self.connection.cursor()
        return self.cursor

    def __exit__(self, error_type, error_value, traceback):
        self.cursor.close()
        self.connection.close()

def ResultIter(cursor, size=100):
    i = 0
    while True:
        part = cursor.fetchmany(size)
        i += len(part)
        print "fetched:", i
        if not part:
            print "break"
            break
        yield part

def StreamingCSV(connection_string, query, header=None):
        if header:
            yield string.join([header],',') + '\n'

        with ODBCContext(connection_string) as cursor:
            cursor.execute(query)
            for part in ResultIter(cursor):
                for row in part:
                    line = string.join([str(r) for r in row],',') + '\n'
                    yield line

For a large query, I can only get 3641 rows out of ~4300 before fetchmany throws a MemoryError.

I've tried a variety of array sizes, as well as fetchone. The error is consistent.

I've also monitored memory usage for the process and I'm not entirely convinced there is a legitimate memory error: physical memory remains stable around 25 megs, virtual at 2.5 gb. At no point do I get any telltale signs of a machine running out of memory.

Any thoughts?

@tristanwietsma
Copy link
Author

Seems to be working fine for pypyodbc.

@mkleehammer
Copy link
Owner

I have pyodbc in a number of 24x7 servers and do not see any evidence of leaks. Is this still occurring? I'm going to close this, but please reopen if you found anything.

@mkleehammer
Copy link
Owner

mkleehammer commented Mar 16, 2017 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants