2

我试图理解这种行为。这绝对不是我所期望的。我有两个程序,一个读者和一个作家。阅读器打开一个 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()

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

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

4

1 回答 1

3

一个简单的解决方法是将“graph.commit()”放在阅读器中的“graph = rdflib.ConjunctiveGraph(...)”行之后。我不确定是什么原因以及为什么在阅读之前提交可以解决此问题。我是这样的:

  • 打开 MySQLdb 连接时,会自动启动一个事务
  • 此事务看不到其他后续事务的更新。
  • “graph.commit()” 冒泡到某个“connection.commit()” 某处丢弃此事务并开始一个新事务。
于 2009-12-07T18:32:16.027 回答