GithubHelp home page GithubHelp logo

Comments (5)

kozlovsky avatar kozlovsky commented on August 28, 2024

Thanks, this is bug, will be fixed today.

from pony.

kozlovsky avatar kozlovsky commented on August 28, 2024

Here is why that happened:

Each Pony ORM session has a cache. This cache contains:

  • objects loaded from the database;
  • query results.

The query results cache was not cleared after commit(), that was a bug. Since you call the same query twice, the second time query result was taken from a cache.

Your last query looks the same, but its generator expression has separate Python code object, so from Pony point of view it is a different query. Because of this, the query result was taken not from the cache, but from database.

Now, after the bug is fixed, query results cache is cleared correctly after commit(), rollback() or flush().


Note, however, that objects cache are still not cleared after commit(), only query results are cleared:

u1 = User[123]  # The object is cached
...
commit()        # Query results cache is cleared, but u1 are still cached
print u1.email  # You can still access cached user object
rollback()      # The cache is invalidated now
print u1.email  # TransactionRolledBack: Object belongs to obsolete cache

To clear object cache you should wrap the function which works with the database with @with_transaction decorator:

@with_transaction
def create_users():
    for i in range(2):
        User(email="email%d" % i, password="password%d" % i)
        # commit() is unnecessary here

create_users()
# commit() and cache clearing will be done automatically

from pony.

iamsk avatar iamsk commented on August 28, 2024

so quick, cool, thx
sth about the cache, i got it, but there is another question
why the parent iterator have effect on the select(generator)

for i in range(2):
select()
will select form db once;

select()
select()
will select form db twice;

from pony.

kozlovsky avatar kozlovsky commented on August 28, 2024

When Pony caches query result it uses id of generator's codeobject as part of the cache key.

In the first case, you call select() with the same generator twice, and on the second iteration Pony uses query result from the cache.

In the second case, you call two different generators, and Python creates two different codeobjects with different ids. Currently Pony doesn't recognizes such generators represent the same query and send two separate queries to the database. In future we may change the way query key is generated and after that Pony will take result of the second select() from the cache.

The next example demonstrates that different generator codeobjects have different ids:

>>> names = [ 'John', 'Smith', 'Bob' ]
>>> def f1():
...     gen1 = (name for name in names)
...     print id(gen1.gi_frame.f_code)
...     
>>> def f2():
...     gen2 = (name for name in names)
...     print id(gen2.gi_frame.f_code)
... 
>>> f1()
54873504
>>> f1()
54873504
>>> f2()
54873144
>>> f2()
54873144
>>> 

from pony.

iamsk avatar iamsk commented on August 28, 2024

ok, thanks for you answer

from pony.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.