Why does my scoped_session create an AttributeError object: "Session" does not have a delete attribute,

I am trying to set up a system that elegantly drops database operations in a separate thread to avoid blocking during Twisted callbacks.

So far, here is my approach:

from contextlib import contextmanager

from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker

from twisted.internet.threads import deferToThread

_engine = create_engine(initialization_string)
Session = scoped_session(sessionmaker(bind=_engine))


@contextmanager
def transaction_context():
    session = Session()
    try:
        yield session
        session.commit()
    except:
        # No need to do session.rollback().  session.remove will do it.
        raise
    finally:
        session.remove()


def threaded(fn):
    @wraps(fn)
    def wrapper(*args, **kwargs):
        return deferToThread(fn, *args, **kwargs)
    return wrapper

This should allow me to wrap the function with a decorator threaded, and then use the context manager transaction_contextin the specified function body. The following is an example:

from __future__ import print_function
from my_lib.orm import User, transaction_context, threaded
from twisted.internet import reactor


@threaded
def get_n_users(n):
    with transaction_context() as session:
        return session.query(User).limit(n).all()

if __name__ == '__main__':
    get_n_users(n).addBoth(len)
    reactor.run()

However, when I run the above script, I get a failure containing the following trace:

Unhandled error in Deferred:
Unhandled Error
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 781, in __bootstrap
    self.__bootstrap_inner()
  File "/usr/lib/python2.7/threading.py", line 808, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 761, in run
    self.__target(*self.__args, **self.__kwargs)
--- <exception caught here> ---
  File "/usr/local/lib/python2.7/dist-packages/twisted/python/threadpool.py", line 191, in _worker
    result = context.call(ctx, function, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/twisted/python/context.py", line 118, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File "/usr/local/lib/python2.7/dist-packages/twisted/python/context.py", line 81, in callWithContext
    return func(*args,**kw)
  File "testaccess.py", line 9, in get_n_users
    return session.query(User).limit(n).all()
  File "/usr/lib/python2.7/contextlib.py", line 24, in __exit__
    self.gen.next()
  File "/home/louis/Documents/Python/knacki/knacki/db.py", line 36, in transaction_context
    session.remove()
exceptions.AttributeError: 'Session' object has no attribute 'remove'

I did not expect this. What am I missing? I did not create an instance scoped_sessioncorrectly?

: - Twisted. , .

+3
1

.remove() Session, Session.

:

scoped_session Session. , , . Session, , , . A thread local - , .

remove scoped_session , , . , scoped_session.__call__, API.

Python script, .

import threading
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker

_engine = create_engine('sqlite:///:memory:')
Session = scoped_session(sessionmaker(_engine))


def scoped_session_demo(remove=False):
    ids = []

    def push_ids():
        thread_name = threading.currentThread().getName()
        data = [thread_name]

        data.append(Session())
        if remove:
            Session.remove()
        data.append(Session())

        ids.append(data)

    t = threading.Thread(target=push_ids)
    t.start()
    t.join()

    push_ids()

    sub_thread, main_thread = ids

    sub_name, sub_session_a, sub_session_b = sub_thread
    main_name, main_session_a, main_session_b = main_thread

    print sub_name, sub_session_a == sub_session_b
    print main_name, main_session_a == main_session_b
    print sub_name, '==', main_name, sub_session_a == main_session_b


print 'Without remove:'
scoped_session_demo()
print 'With remove:'
scoped_session_demo(True)

:

Without remove:
Thread-1 True
MainThread True
Thread-1 == MainThread False
With remove:

Thread-2 False
MainThread False
Thread-2 == MainThread False
+7

All Articles