Is py2neo caching burning me?

2019-08-09 05:24发布

I am running this code:

c = """
    match(r:XX)
    optional match(r)-[]-(m) with count(m) as mc, r match(x)
    return count(x) as all, r, mc
    """
        (snip!)
        while(True):
            tx = remote_graph.cypher.begin()
            res = remote_graph.cypher.execute(c)
            tx.rollback()
            time.sleep(15)
        (snip!)

I know for a fact the XX node's properties are changing every second - there a daemon running. However, when I run this, I always get the same values back in res but for r only - all is changing. The query isn't changing. I wonder if py2neo is noticing this and not executing the query, but is returning me a cached copy? If so, how do I stop this from happening?

EDIT - more info - I ran the above from within ipython.

2条回答
We Are One
2楼-- · 2019-08-09 05:40

Interesting enough, py2neo 'remembers' the node when you return the node:

MATCH (n:Node) RETURN n

But when you return individual properties, they will always be updated:

MATCH (n:Node) RETURN n.value

For your query that means you have to run my_node.pull() when you return the same node twice in a while loop:

while True:
    q = "MATCH (n:Node) RETURN n"
    result = graph.cypher.execute(q)
    my_node = result[0][0]
    my_node.pull()
    print(my_node)

You can also move everything besides the pull() out of the loop:

q = "MATCH (n:Node) RETURN n"
result = graph.cypher.execute(q)
my_node = result[0][0]

while True:
    my_node.pull()
    print(my_node)

Here is a minimal example describing the behaviour: http://paste.ubuntu.com/14015568/

I'm not really sure why py2neo does not return updated node data when you run a new query.

查看更多
戒情不戒烟
3楼-- · 2019-08-09 05:59

What do you mean when you say the node's attributes? Do you mean properties? Or are relationships added/removed as well?

What do you expect to get back in r? Judging from the query, unless the daemon you mention is adding/removing the :XX label to/from nodes, it'll return exactly the same nodes always.

查看更多
登录 后发表回答