Bottle + Cork under Apache "ProgrammingError: <...>"

Bottle + Cork under Apache "ProgrammingError: <...>"

Postby lnkr-a » Tue May 13, 2014 12:32 pm

Good day.

Trying to make authorisation in Bottle using Cork with SQLite backend.

Code: Select all
import bottle
from cork import Cork
from py2neo import neo4j, node, cypher
 
<...> #here is implementation of 'query()' method
 
from cork.backends import SQLiteBackend
try:
    sqldb = SQLiteBackend('example.db', initialize=True)
except:
    sqldb = SQLiteBackend('example.db', initialize=False)
aaa = Cork(backend=sqldb)
authorize = aaa.make_auth_decorator(fail_redirect="/login", role="user")
 
@bottle.route('/')
@bottle.view('login')
def index():
    return
 
@bottle.route('/person_list')
@bottle.view('person_list')
def list():
    html_list = str(aaa.current_user) + "<br><br><ul>"
    persons = query("MATCH (n:person) RETURN (n) ORDER BY n.name")
    for person in persons:
        html_list += bottle.template('person_list_entry', id=person._id, name=person['name'], nickname=person['nickname'])
    html_list += '</ul>'
    return dict(list=html_list)
 
@bottle.post('/login')
def login():
    username = bottle.request.forms.get('username')
    password = bottle.request.forms.get('password')
    aaa.login(username, password, success_redirect='/person_list', fail_redirect='/person_list')


After POST-request handled by login() function decorated by @bottle.post('/login') "Error: 500 Internal Server Error." appears.
Apache logs:

Traceback (most recent call last):, referer: http://localhost/
File “C:\\Python27\\lib\\site-packages\\bottle.py”, line 862, in _handle, referer: http://localhost/
return route.call(**args), referer: http://localhost/
File “C:\\Python27\\lib\\site-packages\\bottle.py”, line 1729, in wrapper, referer: http://localhost/
rv = callback(*a, **ka), referer: http://localhost/
File “D:\\Server\\domains\\localhost\\index.py”, line 50, in login, referer: http://localhost/
aaa.login(username, password, success_redirect='/person_list', fail_redirect='/person_list'), referer: http://localhost/
File “C:\\Python27\\lib\\site-packages\\cork\\cork.py”, line 102, in login, referer: http://localhost/
if username in self._store.users:, referer: http://localhost/
File “C:\\Python27\\lib\\site-packages\\cork\\sqlite_backend.py”, line 52, in __contains__, referer: http://localhost/
row = self._backend.fetch_one(query), referer: http://localhost/
File “C:\\Python27\\lib\\site-packages\\cork\\sqlite_backend.py”, line 230, in fetch_one, referer: http://localhost/
return self._connection.execute(query).fetchone(), referer: http://localhost/
ProgrammingError: SQLite objects created in a thread can only be used in that same thread.The object was created in thread id 5780 and this is thread id 10120, referer: http://localhost/


I understand what is a problem. But so far I don't see any way to solve it. When I have to establish database connection and Cork instance (aaa)?

Hope for answer. Thank you.
Last edited by Yoriz on Tue May 13, 2014 12:42 pm, edited 1 time in total.
Reason: First post lock.
lnkr-a
 
Posts: 2
Joined: Tue May 13, 2014 12:20 pm

Re: Bottle + Cork under Apache "ProgrammingError: <...>"

Postby micseydel » Tue May 13, 2014 5:11 pm

From what I can see, there isn't really a way to fix this, it's just not supported. What source for learning what you're doing are you using? I'm wondering specifically what someone else vouches for vs. what knowledge you had separate from that that lead to this problem. Because surely Bottle + Cork have some proper way of handling a database.

Also: it would be tremendously helpful if you had a simple/dummy implementation for any needed functions, such that the code was runnable and the error reproducible if we were to download it. You should also post that traceback so that even though ideally we don't have to run it locally, we'd be able to see the proper line numbers in the traceback.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1224
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Bottle + Cork under Apache "ProgrammingError: <...>"

Postby lnkr-a » Tue May 13, 2014 6:01 pm

I'm doing it following this HowTo from Cork official site: http://cork.firelet.net/howto.html

Ok. I can remove all Neo4j query functions and all relative to that database(forgot to delete import). Then it will be:

index.py:
Code: Select all
import bottle
from cork import Cork
from py2neo import neo4j, node, cypher
 
from cork.backends import SQLiteBackend
try:
    sqldb = SQLiteBackend('example.db', initialize=True)
except:
    sqldb = SQLiteBackend('example.db', initialize=False)
aaa = Cork(backend=sqldb)
authorize = aaa.make_auth_decorator(fail_redirect="/login", role="user")
 
@bottle.route('/')
@bottle.view('login')
def index():
    return
 
@bottle.route('/person_list')
@bottle.view('person_list')
def list():
    html_list = str(aaa.current_user)
    return dict(list=html_list)
 
@bottle.post('/login')
def login():
    username = bottle.request.forms.get('username')
    password = bottle.request.forms.get('password')
    aaa.login(username, password, success_redirect='/person_list', fail_redirect='/person_list')

login.tpl:
Code: Select all
<a href='/person_list'>List of persons</a>
<form name='log_in_form' method='post' action='/login'>
   <div class='label'>
      <ul>
         <li class='label'>Username:</li>
         <li class='label'>Password:</li>
         <li class='label'>&nbsp;</li>
      </ul>
   </div>
   <div class='input'>
      <ul>
         <li class='input'><input class='text' type='text' name='username'></li>
         <li class='input'><input class='text' type='text' name='password'></li>
         <li class='input' id='submit'><input class='submit' type='submit' name='log_in_button' value='Log in'></li>
      </ul>
   </div>
</form>

adapter.wsgi:
Code: Select all
import sys, os, bottle
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
os.chdir(os.path.dirname(os.path.abspath(__file__)))
import index
application = bottle.default_app()


Other templates aren't used before error. And i think you dont need CSS file.

Apache logs:
[Tue May 13 21:52:17.024705 2014] [:error] [pid 7900:tid 1796] [client 92.241.103.242:53698] Traceback (most recent call last):, referer: http://92.241.103.242/
[Tue May 13 21:52:17.024705 2014] [:error] [pid 7900:tid 1796] [client 92.241.103.242:53698] File "C:\\Python27\\lib\\site-packages\\bottle.py", line 862, in _handle, referer: http://92.241.103.242/
[Tue May 13 21:52:17.024705 2014] [:error] [pid 7900:tid 1796] [client 92.241.103.242:53698] return route.call(**args), referer: http://92.241.103.242/
[Tue May 13 21:52:17.024705 2014] [:error] [pid 7900:tid 1796] [client 92.241.103.242:53698] File "C:\\Python27\\lib\\site-packages\\bottle.py", line 1729, in wrapper, referer: http://92.241.103.242/
[Tue May 13 21:52:17.024705 2014] [:error] [pid 7900:tid 1796] [client 92.241.103.242:53698] rv = callback(*a, **ka), referer: http://92.241.103.242/
[Tue May 13 21:52:17.024705 2014] [:error] [pid 7900:tid 1796] [client 92.241.103.242:53698] File "D:\\Server\\domains\\mabimarket\\index.py", line 28, in login, referer: http://92.241.103.242/
[Tue May 13 21:52:17.024705 2014] [:error] [pid 7900:tid 1796] [client 92.241.103.242:53698] aaa.login(username, password, success_redirect='/person_list', fail_redirect='/person_list'), referer: http://92.241.103.242/
[Tue May 13 21:52:17.024705 2014] [:error] [pid 7900:tid 1796] [client 92.241.103.242:53698] File "C:\\Python27\\lib\\site-packages\\cork\\cork.py", line 102, in login, referer: http://92.241.103.242/
[Tue May 13 21:52:17.024705 2014] [:error] [pid 7900:tid 1796] [client 92.241.103.242:53698] if username in self._store.users:, referer: http://92.241.103.242/
[Tue May 13 21:52:17.024705 2014] [:error] [pid 7900:tid 1796] [client 92.241.103.242:53698] File "C:\\Python27\\lib\\site-packages\\cork\\sqlite_backend.py", line 52, in __contains__, referer: http://92.241.103.242/
[Tue May 13 21:52:17.024705 2014] [:error] [pid 7900:tid 1796] [client 92.241.103.242:53698] row = self._backend.fetch_one(query), referer: http://92.241.103.242/
[Tue May 13 21:52:17.024705 2014] [:error] [pid 7900:tid 1796] [client 92.241.103.242:53698] File "C:\\Python27\\lib\\site-packages\\cork\\sqlite_backend.py", line 230, in fetch_one, referer: http://92.241.103.242/
[Tue May 13 21:52:17.024705 2014] [:error] [pid 7900:tid 1796] [client 92.241.103.242:53698] return self._connection.execute(query).fetchone(), referer: http://92.241.103.242/
[Tue May 13 21:52:17.025705 2014] [:error] [pid 7900:tid 1796] [client 92.241.103.242:53698] ProgrammingError: SQLite objects created in a thread can only be used in that same thread.The object was created in thread id 572 and this is thread id 4896, referer: http://92.241.103.242/
lnkr-a
 
Posts: 2
Joined: Tue May 13, 2014 12:20 pm


Return to Web Development

Who is online

Users browsing this forum: No registered users and 1 guest