Giter Site home page Giter Site logo

Comments (5)

tseaver avatar tseaver commented on June 20, 2024

@ludwigschubert Under the covers, Blob.exists() makes a GET request to the blob's resource URL and converts a 404 response into a False, 20x into a True. This implementation does not fit well with the current Batch design, which fakes the responses inside the context manager, and then applies the sub-responses to the individual targets at exit. See the comment in Blob.exists.

from python-storage.

davidbernat avatar davidbernat commented on June 20, 2024

To be clear: when this was moved to "feature request" by @frankyn , the implied statement is:

there is no way to use blob.exists() within a batch context

Is this correct?

Thanks!

from python-storage.

davidbernat avatar davidbernat commented on June 20, 2024

For @ludwigschubert and those who arrive via Google.

I found a simple fix for this: wrap each call for blob.exists() in a try/catch.

def to_parallelize_catch_exceptions(f, on_error_value, *args):
    try:
        return f(*args)
    except:
        return on_error_value

def parallelize(blobs, function, args_lists=None, on_error_value=None, n_threads=50):
    if args_lists is None:
        args = [(function, on_error_value, blob) for i, blobs in enumerate(stubs)]
   else:
        args = [(function, on_error_value, blob, *args_lists[i]) for i, stub in enumerate(blobs)]
    with multiprocessing.Pool(min(n_threads, len(blobs))) as p:
       values = p.starmap(to_parallelize_catch_exceptions, args)
   return values

def blob_exists(blob):
    blob.exists()

blobs = [....]
with client.batch():
    parallelize(blobs, blob_exists, on_error_value=False)

Of course, there are two layers of abstraction above which are not expressly necessary, but I've copy-pasted my internal code that handles the parallelization for all the cloud client functions. Feel free to comment or DM me for more information as necessary. Cheers.

from python-storage.

tseaver avatar tseaver commented on June 20, 2024

Possible implementation at googleapis/google-cloud-python#8618

from python-storage.

cojenco avatar cojenco commented on June 20, 2024

Thank you folks for providing above workarounds!

The current batch design does not support library methods whose return values depend on the response payload. In this case, the error handling and conversion in blob.exists() is not fully supported in a Batch context.

However, note that a new raise_exception flag is added to Batch via #1043 (pending release in v2.10.0). Setting raise_exception=False allows all exceptions to be included in list of return responses. Although not recommended, it is now possible to get a list of 404 responses calling blob.exists() with storage_client.batch(raise_exception=False)

In addition, we've also added clarifications on the limited Batch support in the python client, see details in #1045

from python-storage.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.