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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:49:06+00:00 2026-05-23T15:49:06+00:00

I have a threaded python application with a long-running mainloop in the background thread.

  • 0

I have a threaded python application with a long-running mainloop in the background thread. This background mainloop is actually a call to pyglet.app.run(), which drives a GUI window and also can be configured to call other code periodically. I need a do_stuff(duration) function to be called at will from the main thread to trigger an animation in the GUI, wait for the animation to stop, and then return. The actual animation must be done in the background thread because the GUI library can’t handle being driven by separate threads.

I believe I need to do something like this:

import threading

class StuffDoer(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.max_n_times = 0
        self.total_n_times = 0
        self.paused_ev = threading.Event()

    def run(self):
        # this part is outside of my control
        while True:
            self._do_stuff()
            # do other stuff

    def _do_stuff(self):
        # this part is under my control
        if self.paused_ev.is_set():
            if self.max_n_times > self.total_n_times:
                self.paused_ev.clear()
        else:
            if self.total_n_times >= self.max_n_times:
                self.paused_ev.set()
        if not self.paused_ev.is_set():
            # do stuff that must execute in the background thread
            self.total_n_times += 1

sd = StuffDoer()
sd.start()

def do_stuff(n_times):
    sd.max_n_times += n_times
    sd.paused_ev.wait_for_clear()   # wait_for_clear() does not exist
    sd.paused_ev.wait()
    assert (sd.total_n_times == sd.max_n_times)

EDIT: use max_n_times instead of stop_time to clarify why Thread.join(duration) won’t do the trick.

From the documentation for threading.Event:

wait([timeout])

Block until the internal flag is true.
If the internal flag is true on entry,
return immediately. Otherwise, block
until another thread calls set() to
set the flag to true, or until the
optional timeout occurs.

I’ve found I can get the behavior I’m looking for if I have a pair of events, paused_ev and not_paused_ev, and use not_paused_ev.wait(). I could almost just use Thread.join(duration), except it needs to only return precisely when the background thread actually registers that the time is up. Is there some other synchronization object or other strategy I should be using instead?

I’d also be open to arguments that I’m approaching this whole thing the wrong way, provided they’re good arguments.

  • 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-23T15:49:06+00:00Added an answer on May 23, 2026 at 3:49 pm

    I ended up using a Queue similar to what @wberry suggested, and making use of Queue.task_done and Queue.wait:

    import Queue
    import threading
    
    class StuffDoer(threading.Thread):
        def __init__(self):
            threading.Thread.__init__(self)
            self.setDaemon(True)
            self.max_n_times = 0
            self.total_n_times = 0
            self.do_queue = Queue.Queue()
    
        def run(self):
            # this part is outside of my control
            while True:
                self._do_stuff()
                # do other stuff
    
        def _do_stuff(self):
            # this part is under my control
            if self.total_n_times >= self.max_n_times:
                try:
                    self.max_n_times += self.do_queue.get(block=False)
                except Queue.Empty, e:
                    pass
            if self.max_n_times > self.total_n_times:
                # do stuff that must execute in the background thread
                self.total_n_times += 1
                if self.total_n_times >= self.max_n_times:
                    self.do_queue.task_done()
    
    sd = StuffDoer()
    sd.start()
    
    def do_stuff(n_times):
        sd.do_queue.put(n_times)
        sd.do_queue.join()
        assert (sd.total_n_times == sd.max_n_times)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing a long-running multi-threaded Python application for Windows, and I want the process
I have a multi-threaded application written in Python in which one thread takes care
I have a multi-threaded Windows application that occasionally deadlocks. Inevitably this happens on a
I have python threaded application + Postgres. I am using Django's ORM to save
I have a multi-threaded application with (4) thread i want to know how much
I have a multi-threaded application which scales well to begin with, but running on
How can I do remote debugging of a multi threaded Python application, running on
I have threaded code where each thread needs to write to the same file.
I have a multi-threaded C++ app which does 3D rendering with the OpenSceneGraph library.
I have an single threaded, embedded application that allocates and deallocates lots and lots

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.