Coder Social home page Coder Social logo

Comments (2)

ross avatar ross commented on August 19, 2024 1

Seems like there's an impedance mismatch between the 2 tools you've chosen and the problem you've set up to solve.

You want to cache individual urls which checkpoint does, but that won't allow you to batch up the misses and take advantage of requests futures. If you want to go that route you'd need to do your own futures and have the submited methods call something that's decorated with checkpoint.

in pseudo-code, haven't seen checkpoint before so no clue whether it can work like this, it may or may not be thread safe.

class Something(object):

    def __init__(self):
        self.sess = Session()  # not a FuturesSession
        self.executor = ThreadPoolExecutor()

    @checkpoint(<whatever goes here, assuming it can decorate a object method>)
    def fetch_url(self, url):
        resp = self.sess.get(url)
        # processing here
        return caption_list

    def fetch_urls(self, urls):
        futures = {u: self.executor.submit(self.fetch_url, u) for u in urls}
        return {u: f.result() for u, f in futures.items()]

s = Something()
results = s.fetch_urls(urls)

Another option would be to manage the caching yourself and use requests futures for the misses.

class Something(object):

    def __init__(self):
        self.sess = FuturesSesson()

    def fetch_urls(self, urls);
        results = {}
        misses = {}
        for url in urls:
            cached = self.get_from_cache(url)
            if cached:
                results[url] = cached
            else:
                misses[url] = self.sess.get(url)
        results.merge({u: self.process_response_and_cache(f.result()) for u, f in misses.items()})
        return results

s = Something()
results = s.fetch_urls(urls)

Anyway both of those are just sketches/starting points. The actual solutions would be more involved and likely cleaner/clearer if i were writing them up for real.

Given that this question is really about how to solve a general problem with caching and async and not how to do something specific to requests_futures or checkpoint SO is probably the right place to ask.

from requests-futures.

vkchan avatar vkchan commented on August 19, 2024

Thanks a lot for your suggestions!

from requests-futures.

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.