Example #1
0
    def connect_async(self, loop=None, timeout=None):
        """Set up async connection on specified event loop or
        on default event loop.
        """
        if self.deferred:
            raise Exception("Error, database not properly initialized "
                            "before opening connection")

        if self._async_conn:
            return
        elif self._async_wait:
            yield from self._async_wait
        else:
            self.loop = loop or asyncio.get_event_loop()
            self._async_wait = asyncio.Future(loop=self.loop)

            conn = self._async_conn_cls(database=self.database,
                                        loop=self.loop,
                                        timeout=timeout,
                                        **self.connect_kwargs_async)

            yield from conn.connect()

            self._task_data = tasklocals.local(loop=self.loop)
            self._async_conn = conn
            self._async_wait.set_result(True)
Example #2
0
    def connect_async(self, loop=None, timeout=None):
        """Set up async connection on specified event loop or
        on default event loop.
        """
        if self.deferred:
            raise Exception("Error, database not properly initialized "
                            "before opening connection")

        if self._async_conn:
            return
        elif self._async_wait:
            yield from self._async_wait
        else:
            self.loop = loop or asyncio.get_event_loop()
            self._async_wait = asyncio.Future(loop=self.loop)

            conn = self._async_conn_cls(
                database=self.database,
                loop=self.loop,
                timeout=timeout,
                **self.connect_kwargs_async)

            yield from conn.connect()

            self._task_data = tasklocals.local(loop=self.loop)
            self._async_conn = conn
            self._async_wait.set_result(True)
Example #3
0
 def test_local_outside_of_task(self):
     mylocal = local(loop=self.loop)
     try:
         mylocal.foo = 1
         self.fail("RuntimeError has not been raised when tryint to use local object outside of a Task")
     except RuntimeError:
         pass
Example #4
0
    def connect_async(self, loop=None, timeout=None):
        """Set up async connection on specified event loop or
        on default event loop.
        """
        if self.deferred:
            raise Exception('Error, database not properly initialized '
                            'before opening connection')

        if not self._async_conn:
            self._loop = loop if loop else asyncio.get_event_loop()
            self._task_data = tasklocals.local(loop=self._loop)
            self._async_conn = self._async_conn_cls(
                self._loop, self.database,
                timeout if timeout else aiopg.DEFAULT_TIMEOUT,
                **self.connect_kwargs_async)
            yield from self._async_conn.connect()
Example #5
0
    def test_local(self):
        mylocal = local(loop=self.loop)

        log1 = []
        log2 = []

        fut1 = asyncio.Future(loop=self.loop)
        fut2 = asyncio.Future(loop=self.loop)

        @asyncio.coroutine
        def task1():
            # get attributes of the local, must be empty at first
            items = list(mylocal.__dict__.items())
            log1.append(items)
            yield from fut1
            # when the fut1 completes, we have already set mylocal.value to "Task 2" in task2
            # it must not be visible in task1, so the __dict__ should still be empty
            items = list(mylocal.__dict__.items())
            log1.append(items)
            mylocal.value = "Task 1"
            items = list(mylocal.__dict__.items())
            log1.append(items)
            # wake up task2 to ensure that value "Task 1" is not visible in task2
            fut2.set_result(True)

        @asyncio.coroutine
        def task2():
            # get attributes of the local, must be empty at first
            items = list(mylocal.__dict__.items())
            log2.append(items)
            mylocal.value = "Task 2"
            # wake up task1
            fut1.set_result(True)
            # wait for task1 to complete
            yield from fut2
            # value "Task 1" must not be visible in this task
            items = list(mylocal.__dict__.items())
            log2.append(items)

        self.loop.run_until_complete(asyncio.wait((task1(), task2()), loop=self.loop))
        # ensure that the values logged are as expected
        self.assertEqual(log1, [[], [], [('value', 'Task 1')]])
        self.assertEqual(log2, [[], [('value', 'Task 2')]])
        # ensure all task local values have been properly cleaned up
        self.assertEqual(object.__getattribute__(mylocal, '_local__impl').dicts, {})
Example #6
0
 def bind(cls, app):
     cls._app = app
     cls._task = tasklocals.local(loop=app.loop)