开发者

Pylons error "No object (name: request) has been registered for this thread" with debug = false

I'm unable to access the request object in my Pylons 0.9.7 controller when I set debug = false in the .ini file. I have the following code:

def run_something(self):
    print('!!! request = %r' % request)
    print('!!! request.params = %r' % request.params)
    yield 'Stuff'

With debugging enabled this works fine and prints out:

!!! request = <Request at 0x9571190 POST http://my_url>
!!! request.params = UnicodeMultiDict([... lots of stuff ...])

If I set debug = false I get the following:

!!! request = <paste.registry.StackedObjectProxy object at 0x4093790>
Error - <type 'exceptions.TypeError'>: No object (name: request) has been registered for this thread

The stack 开发者_Go百科trace confirms that the error is on the print('!!! request.params = %r' % request.params) line.

I'm running it using the Paste server and these two lines are the very first lines in my controller method.

This only occurs if I have yield statements in the method (even though the statements aren't reached). I'm guessing Pylons sees that it's a generator method and runs it on some other thread. My questions are:

  1. How do I make it work with debug = false ?
  2. Why does it work with debug = true ? Obviously this is quite a dangerous bug, since I normally develop with debug = true, so it can go unnoticed during development.


You should not yield from action directly. Try making inner function and returning func():

def run_something(self):
    request = pylons.request._current_obj()
    def func():
        print('!!! request = %r' % request)
        print('!!! request.params = %r' % request.params)
        yield 'Stuff'
    return func()


When you set up your middleware, enable streaming support in RegistryManager:

app = RegistryManager(app, streaming=True)

This keeps the registry (aoo_globals etc.) alive until your response has finished iterating.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜