title: RDFlib with Fuseki running queries very slowly on local server
body: <p>I have a small wsgi app, running a local Cherry server, and I use the RDFlib to turn human language queries into SPARQL queries for querying a ttl file loaded to fuseki. It works, but it is incredibly slow! The previous version of this script accepted SPARQL queries directly, so I didn't use the RDFlib, and it worked very fast! Is there is something wrong with the way I use RDFlib which makes it so slow??</p>
  
  <pre><code>from abc import ABCMeta, abstractmethod, abstractproperty
  from collections import OrderedDict
  from threading import Thread
  from time import sleep
  from cherrypy import engine
  from cherrypy.wsgiserver import CherryPyWSGIServer
  from werkzeug.wsgi import DispatcherMiddleware
  from werkzeug.debug import DebuggedApplication
  from werkzeug.wsgi import SharedDataMiddleware
  from werkzeug.wrappers import Response, Request
  from requests import get, post, RequestException
  from jinja2 import Environment, FileSystemLoader
  import os
  from SPARQLWrapper import SPARQLWrapper, JSON
  import rdflib
  
  
  __author__ = 'authorname'
  
  templates_dir = os.path.abspath('templates')
  static_dir = os.path.abspath('static')
  
  class RdfDemoApp(object):
      def __init__(self, sparql_endpoint_address):
          self._sparql_endpoint_address = sparql_endpoint_address
          self._jinja_env = Environment(loader=FileSystemLoader(templates_dir), autoescape=True)
  
      def render_template(self, template, **params):
          t = self._jinja_env.get_template(template)
          return t.render(params)
  
      def _app(self, environ, start_response):
          request = Request(environ)
          if 'query' in request.args.keys():
              query_string = "'"+request.args['query']+"'"
              print query_string
  
              results = g.query("""PREFIX pers: &lt;http://blabla.com/Register/schemas/persons/&gt;
              SELECT ?person ?sibling ?sibforname
              WHERE { 
                 ?person pers:name ?name .
                   ?name  pers:forename """+query_string+""" .
                  ?person pers:siblingOf ?sibling .
                  ?sibling pers:name ?sibname .
                  ?sibname pers:forename ?sibforname .
                   ?sibname pers:type "std"  } """)#, format = "JSON")
              for row in results:
                  print row
              header = []
              i=0
              for item in results:
                  while i in range(len(item)):
                      for x in item:
                          header.append(x)
                          i+=1
              quer = query_string
              response = Response(self.render_template('results_rdf.html', results=results, header = header, query = quer, static_dir = static_dir ), mimetype='text/html')
  
          else:
              response = Response(self.render_template('form_rdf.html'), mimetype='text/html')
  
          return response(environ, start_response)
  
      def __call__(self, environ, start_response):
          app = SharedDataMiddleware(DebuggedApplication(self._app, evalex=True), {
              '/static': static_dir
          })
          return app(environ, start_response)
  
  if __name__ == '__main__':
      g=rdflib.Graph()
      g.parse("/Users/username/Documents/pers_file_new.ttl", format='n3')
      wsgi_app = RdfDemoApp("http://localhost:3030/ds/query")
      try:
          server = CherryPyWSGIServer(('127.0.0.1',10001), wsgi_app)
          server.start()
  
      except KeyboardInterrupt:
          server.stop()
          print "Logged out"
  </code></pre>