2009-12-07 26 views
2

我想了解這種行爲。這絕對不是我所期望的。我有兩個程序,一個讀者和一個作家。讀者打開一個RDFlib圖存儲,然後執行每2秒rdflib圖未更新。爲什麼?

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 " - - - - - - - - " 

第二個程序的查詢,增加東西向triplestore

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() 

我希望看到的結果數量作家當我使用作者提交的東西時,閱讀器會增加,但這不會發生。讀者繼續返回與開始時相同的結果。但是,如果我停止閱讀器並重新啓動它,則會顯示新的結果。

有人知道我做錯了什麼嗎?

回答

3

一個簡單的解決方法是將「graph.commit()」放在閱讀器中的「graph = rdflib.ConjunctiveGraph(...)」行之後。 我不確定是什麼原因以及爲什麼在閱讀前提交修復此問題。我猜測是:

  • 當打開MySQLdb的連接,事務會自動
  • 入門本次交易不看其他,以後交易的更新。
  • 「graph.commit()」冒泡到某個「connection.commit()」某處丟棄該事務並啓動一個新的事務。
+0

您認爲這是一個錯誤或功能? – 2009-12-08 01:55:54

+0

好吧,我瀏覽了消息來源,它確實看起來在一次交易中發生了一切。這是保存事務的商店,因此每次重新創建ConjunctiveGraph都不會執行提交,並且它會始終在創建商店實例時看到圖形。 – 2009-12-08 03:14:59

+0

它仍然是這樣嗎? – 2013-08-09 23:51:52

相關問題