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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:54:57+00:00 2026-06-04T11:54:57+00:00

I need a simple class or function that takes a test (callable object which

  • 0

I need a simple class or function that takes a test (callable object which returns True or False) and a function to be called when the test is True, potentially doing the whole thing in a different thread maybe. something like this:

nums = []
t = TestClass(test=(lambda: len(nums) > 5),
              func=(lambda: sys.stdout.write('condition met'))

for n in range(10):
    nums.append(n)
    time.sleep(1) 

#after 6 loops, the message gets printed on screen.

Any help is appreciated. (Please, nothing too complicated since I am still a beginner)

  • 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-04T11:54:59+00:00Added an answer on June 4, 2026 at 11:54 am

    You’re correct in thinking you might need a separate thread to check a condition in the background. In this separate thread, you also have to decide how often you want to check (there would be other ways to do this, but this way requires the least changes to the code you’ve shown).

    My answer just uses a function, but you could easily use a class instead if you like:

    from threading import Thread
    import time
    import sys    
    
    def myfn(test, callback):
    
        while not test():  # check if the first function passed in evaluates to True
            time.sleep(.001)  # we need to wait to give the other thread time to run.
        callback() # test() is True, so call callback.
    
    nums = []
    
    t = Thread(target=myfn, args=(lambda: len(nums) > 5, 
               lambda: sys.stdout.write('condition met')))
    t.start() # start the thread to monitor for nums length changing
    
    for n in range(10):
        nums.append(n)
        print nums  # just to show you the progress
        time.sleep(1) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple Java class that I need to serialize to be stored
I need a simple proxy PHP function/script that can download a file from a
I tried to make a function that takes a vector for an argument: class
I need a function which takes an arbitrary number of arguments (All of the
I need a simple application, preferably a cross-platform one, that enables sending of files
I need an simple way to check whether a string that is sent to
I need a simple forum backed by a SQLite 3 database which I can
How do I allow a class with a copy constructor that takes a non-const
I have a C# class library that contains methods that need to be used
I'm making this function that takes a string input, converts it to an int,

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.