zope.globalrequest

raw JSON →
3.0 verified Mon Apr 27 auth: no python

Provides a global way to retrieve the currently active request, commonly used in Zope/ Pyramid applications. Current version: 3.0, requires Python >=3.9. Maintained by the Zope Foundation.

pip install zope.globalrequest
error ImportError: cannot import name 'getRequest' from 'zope.publisher.browser'
cause Deprecated import path removed in zope.publisher.
fix
Use 'from zope.globalrequest import getRequest' instead.
error TypeError: setRequest() missing 1 required positional argument: 'request'
cause Calling setRequest() without an argument.
fix
Pass a request object: setRequest(request_obj).
gotcha Thread safety: setRequest/getRequest are thread-safe only if config.SET_AS_COROUTINE_LOCAL is False (default). In async contexts, use async local aware patterns.
fix Set zope.globalrequest.config.SET_AS_COROUTINE_LOCAL = True for async support.
deprecated The compatibility import from zope.publisher.browser is deprecated. Always import directly from zope.globalrequest.
fix Change imports to 'from zope.globalrequest import getRequest'.
gotcha Mixing threads and coroutines: if you use both threading and asyncio, you must decide on a single local storage strategy. The default thread-local may cause cross-request leaks.
fix Configure via zope.globalrequest.config.set_local_storage('coroutine') or 'thread' explicitly.

Set and retrieve the current global request.

from zope.globalrequest import setRequest, getRequest

class FakeRequest:
    pass

request = FakeRequest()
setRequest(request)
active = getRequest()
print(active)  # <__main__.FakeRequest object at ...>