代码之家  ›  专栏  ›  技术社区  ›  Stefano Borini

RDflib图未更新。为什么?

  •  2
  • Stefano Borini  · 技术社区  · 16 年前

    我在努力理解这种行为。这绝对不是我所期望的。我有两个节目,一个读者,一个作家。读卡器打开一个RDflib图形存储,然后每2秒执行一次查询

    import rdflib
    import random
    from rdflib import store
    import time
    
    default_graph_uri = "urn:uuid:a19f9b78-cc43-4866-b9a1-4b009fe91f52"
    
    s = rdflib.plugin.get('MySQL', store.Store)('rdfstore')
    
    config_string = "host=localhost,password=foo,user=foo,db=foo"
    rt = s.open(config_string,create=False)
    if rt != store.VALID_STORE:
        s.open(config_string,create=True)
    
    
    while True:
        graph = rdflib.ConjunctiveGraph(s, identifier = rdflib.URIRef(default_graph_uri))
        rows = graph.query("SELECT ?id ?value { ?id <http://localhost#ha> ?value . }")
        for r in rows:
            print r[0], r[1]
        time.sleep(2)
        print " - - - - - - - - "
    

    第二个程序是一个向三重存储库添加内容的编写器。

    import rdflib
    import random
    from rdflib import store
    
    default_graph_uri = "urn:uuid:a19f9b78-cc43-4866-b9a1-4b009fe91f52"
    
    s = rdflib.plugin.get('MySQL', store.Store)('rdfstore')
    
    config_string = "host=localhost,password=foo,user=foo,db=foo"
    rt = s.open(config_string,create=False)
    if rt != store.VALID_STORE:
        s.open(config_string,create=True)
    
    graph = rdflib.ConjunctiveGraph(s, identifier = rdflib.URIRef(default_graph_uri))
    
    graph.add( ( 
                rdflib.URIRef("http://localhost/"+str(random.randint(0,100))), 
                rdflib.URIRef("http://localhost#ha"),
                rdflib.Literal(str(random.randint(0,100)))
                ) 
                )
    graph.commit()
    

    当我使用编写器提交内容时,我希望看到阅读器上的结果数量会增加,但这种情况不会发生。读卡器继续返回与启动时相同的结果。但是,如果我停止读卡器并重新启动它,就会出现新的结果。

    有人知道我做错了什么吗?

    1 回复  |  直到 16 年前
        1
  •  3
  •   Pēteris Caune    16 年前

    一个简单的解决方法是将“graph.commit()”放在读卡器中“graph=rdflib.concepegraph(…)”行的后面。 我不知道原因是什么,为什么在阅读前提交可以解决这个问题。我是 猜测 那就是:

    • 打开mysqldb连接时,事务自动启动
    • 此事务看不到其他以后事务的更新。
    • “graph.commit()”冒泡到某个“connection.commit()”位置,该位置丢弃此事务并启动新事务。