{"action":"created","issue":{"url":"https://api.github.com/repos/mitsuhiko/flask/issues/1576","labels_url":"https://api.github.com/repos/mitsuhiko/flask/issues/1576/labels{/name}","comments_url":"https://api.github.com/repos/mitsuhiko/flask/issues/1576/comments","events_url":"https://api.github.com/repos/mitsuhiko/flask/issues/1576/events","html_url":"https://github.com/mitsuhiko/flask/issues/1576","id":109127624,"number":1576,"title":"an context confuse when i using the celery ","user":{"login":"Smalinuxer","id":8388185,"avatar_url":"https://avatars.githubusercontent.com/u/8388185?v=3","gravatar_id":"","url":"https://api.github.com/users/Smalinuxer","html_url":"https://github.com/Smalinuxer","followers_url":"https://api.github.com/users/Smalinuxer/followers","following_url":"https://api.github.com/users/Smalinuxer/following{/other_user}","gists_url":"https://api.github.com/users/Smalinuxer/gists{/gist_id}","starred_url":"https://api.github.com/users/Smalinuxer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Smalinuxer/subscriptions","organizations_url":"https://api.github.com/users/Smalinuxer/orgs","repos_url":"https://api.github.com/users/Smalinuxer/repos","events_url":"https://api.github.com/users/Smalinuxer/events{/privacy}","received_events_url":"https://api.github.com/users/Smalinuxer/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-09-30T16:14:24Z","updated_at":"2015-09-30T17:34:29Z","closed_at":null,"body":"it appear in when i using the celery with flask_sqlalchemy \r\n\r\ni am using the celery with context  by  http://flask.pocoo.org/docs/0.10/patterns/celery/\r\nbut i find \"copy_current_request_context\" is not work , because the context is typeof the AppContext not the RequestContext . so when invoke @copy_current_request_context , it raise the \r\n\r\nRuntimeError('This decorator can only be used at local scopes '\r\n            'when a request context is on the stack.  For instance within '\r\n            'view functions.')\r\n\r\nso i try to  replace the RequestContext with AppContext in @copy_current_request_context , it work!\r\nthe code : \r\n\r\n\r\n\r\n\r\n    def copy_current_request_context(f):\r\n\r\n        top = _app_ctx_stack.top\r\n        if top is None:\r\n            raise RuntimeError('This decorator can only be used at local scopes '\r\n                'when a request context is on the stack.  For instance within '\r\n                'view functions.')\r\n        reqctx = top.copy()\r\n        def wrapper(*args, **kwargs):\r\n            with reqctx:\r\n                return f(*args, **kwargs)\r\n        return update_wrapper(wrapper, f)\r\n\r\n\r\n\r\n\r\n\r\n    class AppContext(object):\r\n\r\n        def __init__(self, app):\r\n            self.app = app\r\n            self.url_adapter = app.create_url_adapter(None)\r\n            self.g = app.app_ctx_globals_class()\r\n\r\n            # Like request context, app contexts can be pushed multiple times\r\n            # but there a basic \"refcount\" is enough to track them.\r\n            self._refcnt = 0\r\n\r\n        def copy(self):\r\n  \r\n            mapp = self.__class__(self.app,)\r\n            return mapp\r\n\r\n        def push(self):\r\n            \"\"\"Binds the app context to the current context.\"\"\"\r\n            self._refcnt += 1\r\n            _app_ctx_stack.push(self)\r\n            appcontext_pushed.send(self.app)\r\n\r\n        def pop(self, exc=None):\r\n            \"\"\"Pops the app context.\"\"\"\r\n            self._refcnt -= 1\r\n            if self._refcnt <= 0:\r\n                if exc is None:\r\n                    exc = sys.exc_info()[1]\r\n                self.app.do_teardown_appcontext(exc)\r\n            rv = _app_ctx_stack.pop()\r\n            assert rv is self, 'Popped wrong app context.  (%r instead of %r)' \\\r\n                % (rv, self)\r\n            appcontext_popped.send(self.app)\r\n\r\n        def __enter__(self):\r\n            self.push()\r\n            return self\r\n\r\n        def __exit__(self, exc_type, exc_value, tb):\r\n            self.pop(exc_value)\r\n\r\n\r\n\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/mitsuhiko/flask/issues/comments/144485078","html_url":"https://github.com/mitsuhiko/flask/issues/1576#issuecomment-144485078","issue_url":"https://api.github.com/repos/mitsuhiko/flask/issues/1576","id":144485078,"user":{"login":"davidism","id":1242887,"avatar_url":"https://avatars.githubusercontent.com/u/1242887?v=3","gravatar_id":"","url":"https://api.github.com/users/davidism","html_url":"https://github.com/davidism","followers_url":"https://api.github.com/users/davidism/followers","following_url":"https://api.github.com/users/davidism/following{/other_user}","gists_url":"https://api.github.com/users/davidism/gists{/gist_id}","starred_url":"https://api.github.com/users/davidism/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/davidism/subscriptions","organizations_url":"https://api.github.com/users/davidism/orgs","repos_url":"https://api.github.com/users/davidism/repos","events_url":"https://api.github.com/users/davidism/events{/privacy}","received_events_url":"https://api.github.com/users/davidism/received_events","type":"User","site_admin":false},"created_at":"2015-09-30T17:34:29Z","updated_at":"2015-09-30T17:34:29Z","body":"You can only use that decorator within an app context, since it relies on an app context being present.  I'm not clear where you even got that code from.  The whole point of the code in the docs is so that the workers will execute tasks in an app context without having to pass it explicitly.  If your task requires information from the request, pass it manually rather than trying to copy the request context."}}