Ejemplo n.º 1
0
class TwistedEngine(object):
    def __init__(self,
                 pool,
                 dialect,
                 url,
                 reactor=None,
                 create_worker=_threaded_worker,
                 **kwargs):
        if reactor is None:
            raise TypeError("Must provide a reactor")

        self._engine = Engine(pool, dialect, url, **kwargs)
        self._reactor = reactor
        self._create_worker = create_worker
        self._engine_worker = self._create_worker()

    def _defer_to_engine(self, f, *a, **k):
        return _defer_to_worker(self._reactor.callFromThread,
                                self._engine_worker, f, *a, **k)

    @property
    def dialect(self):
        return self._engine.dialect

    @property
    def _has_events(self):
        return self._engine._has_events

    @property
    def _execution_options(self):
        return self._engine._execution_options

    def _should_log_info(self):
        return self._engine._should_log_info()

    def execute(self, *args, **kwargs):
        return (self._defer_to_engine(self._engine.execute, *args,
                                      **kwargs).addCallback(
                                          TwistedResultProxy,
                                          self._defer_to_engine))

    def has_table(self, table_name, schema=None):
        return self._defer_to_engine(self._engine.has_table, table_name,
                                     schema)

    def table_names(self, schema=None, connection=None):
        if connection is not None:
            connection = connection._connection
        return self._defer_to_engine(self._engine.table_names, schema,
                                     connection)

    def connect(self):
        worker = self._create_worker()
        return (_defer_to_worker(self._reactor.callFromThread, worker,
                                 self._engine.connect).addCallback(
                                     TwistedConnection, self, worker))
Ejemplo n.º 2
0
class TwistedEngine(object):
    def __init__(self, pool, dialect, url, reactor=None, thread_pool=None,
                 **kwargs):
        if reactor is None:
            raise TypeError("Must provide a reactor")

        self._engine = Engine(pool, dialect, url, **kwargs)
        self._reactor = reactor
        if thread_pool is None:
            thread_pool = reactor.getThreadPool()
        self._tpool = thread_pool

    def _defer_to_thread(self, f, *args, **kwargs):
        return deferToThreadPool(
            self._reactor, self._tpool, f, *args, **kwargs
        )

    @property
    def dialect(self):
        return self._engine.dialect

    @property
    def _has_events(self):
        return self._engine._has_events

    @property
    def _execution_options(self):
        return self._engine._execution_options

    def _should_log_info(self):
        return self._engine._should_log_info()

    def connect(self):
        d = self._defer_to_thread(self._engine.connect)
        d.addCallback(TwistedConnection, self)
        return d

    def execute(self, *args, **kwargs):
        d = self._defer_to_thread(self._engine.execute, *args, **kwargs)
        d.addCallback(TwistedResultProxy, self)
        return d

    def has_table(self, table_name, schema=None):
        return self._defer_to_thread(
            self._engine.has_table, table_name, schema)

    def table_names(self, schema=None, connection=None):
        if connection is not None:
            connection = connection._connection
        return self._defer_to_thread(
            self._engine.table_names, schema, connection)
Ejemplo n.º 3
0
class TwistedEngine(object):
    def __init__(self, pool, dialect, url, reactor=None, thread_pool=None,
                 **kwargs):
        if reactor is None:
            raise TypeError("Must provide a reactor")

        self._engine = Engine(pool, dialect, url, **kwargs)
        self._reactor = reactor
        if thread_pool is None:
            thread_pool = reactor.getThreadPool()
        self._tpool = thread_pool

    def _defer_to_thread(self, f, *args, **kwargs):
        return deferToThreadPool(
            self._reactor, self._tpool, f, *args, **kwargs
        )

    @property
    def dialect(self):
        return self._engine.dialect

    @property
    def _has_events(self):
        return self._engine._has_events

    @property
    def _execution_options(self):
        return self._engine._execution_options

    def _should_log_info(self):
        return self._engine._should_log_info()

    def connect(self):
        d = self._defer_to_thread(self._engine.connect)
        d.addCallback(TwistedConnection, self)
        return d

    def execute(self, *args, **kwargs):
        d = self._defer_to_thread(self._engine.execute, *args, **kwargs)
        d.addCallback(TwistedResultProxy, self)
        return d

    def has_table(self, table_name, schema=None):
        return self._defer_to_thread(
            self._engine.has_table, table_name, schema)

    def table_names(self, schema=None, connection=None):
        if connection is not None:
            connection = connection._connection
        return self._defer_to_thread(
            self._engine.table_names, schema, connection)
Ejemplo n.º 4
0
class TwistedEngine(object):
    def __init__(self, pool, dialect, url, reactor=None, **kwargs):
        if reactor is None:
            raise TypeError("Must provide a reactor")

        super(TwistedEngine, self).__init__()
        self._engine = Engine(pool, dialect, url, **kwargs)
        self._reactor = reactor

    def _defer_to_thread(self, f, *args, **kwargs):
        tpool = self._reactor.getThreadPool()
        return deferToThreadPool(self._reactor, tpool, f, *args, **kwargs)

    @property
    def dialect(self):
        return self._engine.dialect

    @property
    def _has_events(self):
        return self._engine._has_events

    @property
    def _execution_options(self):
        return self._engine._execution_options

    def _should_log_info(self):
        return self._engine._should_log_info()

    def connect(self):
        d = self._defer_to_thread(self._engine.connect)
        d.addCallback(TwistedConnection, self)
        return d

    def execute(self, *args, **kwargs):
        d = self._defer_to_thread(self._engine.execute, *args, **kwargs)
        d.addCallback(TwistedResultProxy, self)
        return d
Ejemplo n.º 5
0
class TwistedEngine(object):
    def __init__(self,
                 pool,
                 dialect,
                 url,
                 reactor=None,
                 create_worker=_threaded_worker,
                 **kwargs):

        if reactor is None:
            raise TypeError("Must provide a reactor")

        self._engine = Engine(pool, dialect, url, **kwargs)
        self._reactor = reactor
        self._create_worker = create_worker
        self._engine_worker = self._create_worker()

    @classmethod
    def from_sqlalchemy_engine(cls,
                               reactor,
                               engine,
                               create_worker=_threaded_worker):
        # Leaving the existing __init__ in place for compatibility reasons,
        # this is a completely alternate constructor.
        self = cls.__new__(cls)
        self._reactor = reactor
        self._engine = engine
        self._create_worker = create_worker
        self._engine_worker = create_worker()
        return self

    def _defer_to_engine(self, f, *a, **k):
        return _defer_to_worker(self._reactor.callFromThread,
                                self._engine_worker, f, *a, **k)

    @property
    def dialect(self):
        return self._engine.dialect

    @property
    def _has_events(self):
        return self._engine._has_events

    @property
    def _execution_options(self):
        return self._engine._execution_options

    def _should_log_info(self):
        return self._engine._should_log_info()

    def execute(self, *args, **kwargs):
        return (self._defer_to_engine(self._engine.execute, *args,
                                      **kwargs).addCallback(
                                          TwistedResultProxy,
                                          self._defer_to_engine))

    def has_table(self, table_name, schema=None):
        return self._defer_to_engine(self._engine.has_table, table_name,
                                     schema)

    def table_names(self, schema=None, connection=None):
        if connection is not None:
            connection = connection._connection
        return self._defer_to_engine(self._engine.table_names, schema,
                                     connection)

    def connect(self):
        worker = self._create_worker()
        return (_defer_to_worker(self._reactor.callFromThread, worker,
                                 self._engine.connect).addCallback(
                                     TwistedConnection, self, worker))