issues working with python generators and openstack swift client

前端 未结 1 622
小蘑菇
小蘑菇 2021-02-18 19:28

I\'m having a problem with Python generators while working with the Openstack Swift client library.

The problem at hand is that I am trying to retrieve a large string of

相关标签:
1条回答
  • 2021-02-18 20:01

    In your get_object method, you're assigning the return value of _object_body() to the contents variable. However, that variable is also the one that holds your actual data, and it's used early on in _object_body.

    The problem is that _object_body is a generator function (it uses yield). Therefore, when you call it, it produces a generator object, but the code of the function doesn't start running until you iterate over that generator. Which means that when the function's code actually starts running (the for loop in _test_stream), it's long after you've reassigned contents = _object_body().

    Your stream = StringIO(contents) therefore creates a StringIO object containing the generator object (hence your error message), not the data.

    Here's a minimal reproduction case that illustrates the problem:

    def foo():
        contents = "Hello!"
    
        def bar():
            print contents
            yield 1
    
        # Only create the generator. This line runs none of the code in bar.
        contents = bar()
    
        print "About to start running..."
        for i in contents:
            # Now we run the code in bar, but contents is now bound to 
            # the generator object. So this doesn't print "Hello!"
            pass
    
    0 讨论(0)
提交回复
热议问题