Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6542911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:17:00+00:00 2026-05-25T11:17:00+00:00

I have a generator that takes a long time for each iteration to run.

  • 0

I have a generator that takes a long time for each iteration to run. Is there a standard way to have it yield a value, then generate the next value while waiting to be called again?

The generator would be called each time a button is pressed in a gui and the user would be expected to consider the result after each button press.

EDIT: a workaround might be:

def initialize():
    res = next.gen()

def btn_callback()
    display(res)
    res = next.gen()
    if not res:
       return
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:17:00+00:00Added an answer on May 25, 2026 at 11:17 am

    If I wanted to do something like your workaround, I’d write a class like this:

    class PrefetchedGenerator(object):
        def __init__(self, generator):
             self._data = generator.next()
             self._generator = generator
             self._ready = True
    
        def next(self):
            if not self._ready:
                self.prefetch()
            self._ready = False
            return self._data
    
        def prefetch(self):
            if not self._ready:
                self._data = self._generator.next()
                self._ready = True
    

    It is more complicated than your version, because I made it so that it handles not calling prefetch or calling prefetch too many times. The basic idea is that you call .next() when you want the next item. You call prefetch when you have “time” to kill.

    Your other option is a thread..

    class BackgroundGenerator(threading.Thread):
        def __init__(self, generator):
            threading.Thread.__init__(self)
            self.queue = Queue.Queue(1)
            self.generator = generator
            self.daemon = True
            self.start()
    
        def run(self):
            for item in self.generator:
                self.queue.put(item)
            self.queue.put(None)
    
        def next(self):
                next_item = self.queue.get()
                if next_item is None:
                     raise StopIteration
                return next_item
    

    This will run separately from your main application. Your GUI should remain responsive no matter how long it takes to fetch each iteration.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have 1000 threads that hit a web service and time how long the
I have a WCF service hosted in IIS which takes a long time (around
I have a script that takes a table name and generates a control file
I have an application that takes some input and generates configuration files as output.
I have a generator that generates a series, for example: def triangle_nums(): '''Generates a
I have a pseudo random number generator (PRNG) class that I want to unit
I have a test generator written in Perl. It generates tests that connect to
I have a program that uses the mt19937 random number generator from boost::random. I
I'm starting to learn Python and I've come across generator functions, those that have
We have written a custom windows service (VB.NET, .NET Framework 2.0) that takes a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.