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 8996615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:46:41+00:00 2026-06-15T23:46:41+00:00

I have a long running task that must be guided by external code. But

  • 0

I have a long running task that must be guided by external code. But external code need some information about this task. This is my homebrew example:

def longtask(self):
    yield self.get_step_length(1)
    for x in self.perform_step(1):
        ...
        yield x.id

    yield self.get_step_length(2)
    for x in self.perform_step(2):
        ...
        yield x.value

...
# call site
generator = self.longtask()
step1len = generator.Next()
step1pb = ProgressBar('Step 1', step1len)
# pull only step 1 items
for index, id in itertools.izip(xrange(0, step1len), generator):
    step1pb.update(index)
    ...do something with id

step2len = generator.Next()
step2pb = ProgressBar('Step 2', step1len)
# pull only step 1 items
for index, value in itertools.izip(xrange(0, step1len), generator):
    step2pb.update(index)
    ... do something other with value

Is it right to use such a complex generator protocols in python, or I need to refactor this code?

  • 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-06-15T23:46:42+00:00Added an answer on June 15, 2026 at 11:46 pm

    I’d refactor this to returning separate generators; you can use nested functions:

    def longtask(self):
        def step_generator(step):
            for x in self.perform_step(step):
                ...
                yield x.id
    
        yield step_length_1, step_one_generator(1)
        yield step_length_2, step_one_generator(2)
    
    generators = self.longtask()
    for counter, (steplength, stepgen) in enumerate(generators):
        ProgressBar('Step %d' % counter, steplength)
        for index, value in enumerate(stepgen):
            # ....
    

    Now you can also use the enumerate() function to add numbers to the items; that is much more readable than zipping together an xrange() and the generator.

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

Sidebar

Related Questions

I have a long-running process that must run every five minutes, but more than
I have a long running task that I need to implement on a webpage.
I have a long running Async Task that sends some data to my server
I have an Activity with a button that starts a long running task. I
I have some long running scripts with breaks requiring input/interaction to continue but when
I have a long-running cleanup operation that I need to perform in onDestroy() of
I occasionally have some long running AJAX requests in my Wicket application. When this
i have long running task that gets called using jquery ajax. i am using
I have a long running, compute and disk intensive task that needs to run
I have a long-running Task that I've implemented using the Task Parallel Library. When

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.