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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:50:38+00:00 2026-05-17T22:50:38+00:00

I’m writing an application that listens for sound events (using messages passed in with

  • 0

I’m writing an application that listens for sound events (using messages passed in with Open Sound Control), and then based on those events pauses or resumes program execution. My structure works most of the time but always bombs out in the main loop, so I’m guessing it’s a thread issue. Here’s a generic, simplified version of what I’m talking about:

import time, threading

class Loop():
    aborted = False

    def __init__(self):
        message = threading.Thread(target=self.message, args=((0),))
        message.start()

        loop = threading.Thread(target=self.loop)
        loop.start()


    def message(self,val):

        if val > 1:
            if not self.aborted:
                self.aborted = True
                # do some socket communication            
            else:
                self.aborted = False
                # do some socket communication


    def loop(self):
        cnt = 0

        while True:
            print cnt
            if self.aborted:
                while self.aborted:
                    print "waiting"
                    time.sleep(.1);
            cnt += 1


class FakeListener():
    def __init__(self,loop):
        self.loop = loop
        listener = threading.Thread(target=self.listener)
        listener.start()

    def listener(self):
        while True:
            loop.message(2)
            time.sleep(1)



if __name__ == '__main__':

    loop = Loop()

    #fake listener standing in for the real OSC event listener
    listener = FakeListener(loop)

Of course, this simple code seems to work great, so it’s clearly not fully illustrating my real code, but you get the idea. What isn’t included here is also the fact that on each loop pause and resume (by setting aborted=True/False) results in some socket communication which also involves threads.

What always happens in my code is that the main loop doesn’t always pickup where it left off after a sound event. It will work for a number of events but then eventually it just doesn’t answer.

Any suggestions for how to structure this kind of communication amongst threads?

UPDATE:

ok, i think i’ve got it. here’s a modification that seems to work. there’s a listener thread that periodically puts a value into a Queue object. there’s a checker thread that keeps checking the queue looking for the value, and once it sees it sets a boolean to its opposite state. that boolean value controls whether the loop thread continues or waits.

i’m not entirely sure what the q.task_done() function is doing here, though.

import time, threading
import Queue

q = Queue.Queue(maxsize = 0)

class Loop():
    aborted = False

    def __init__(self):
        checker = threading.Thread(target=self.checker)
        checker.setDaemon(True)
        checker.start()

        loop = threading.Thread(target=self.loop)
        loop.start()


    def checker(self):
        while True:
            if q.get() == 2:
                q.task_done()
                if not self.aborted:
                    self.aborted = True
                else:
                    self.aborted = False


    def loop(self):
        cnt = 0

        while cnt < 40:
            if self.aborted:
                while self.aborted:
                    print "waiting"
                    time.sleep(.1)
            print cnt
            cnt += 1
            time.sleep(.1)


class fakeListener():

    def __init__(self):
        listener = threading.Thread(target=self.listener)
        listener.setDaemon(True)
        listener.start()

    def listener(self):
        while True:
            q.put(2)
            time.sleep(1)



if __name__ == '__main__':

    #fake listener standing in for the real OSC event listener
    listener = fakeListener()

    loop = Loop()
  • 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-17T22:50:38+00:00Added an answer on May 17, 2026 at 10:50 pm

    Umm.. I don’t completely understand your question but i’ll do my best to explain what I think you need to fix your problems.

    1) The thread of your Loop.loop function should be set as a daemon thread so that it exits with your main thread (so you don’t have to kill the python process every time you want to shut down your program). To do this just put loop.setDaemon(True) before you call the thread’s “start” function.

    2)The most simple and fail-proof way to communicate between threads is with a Queue. On thread will put an item in that Queue and another thread will take an item out, do something with the item and then terminate (or get another job)

    In python a Queue can be anything from a global list to python’s built-in Queue object. I recommend the python Queue because it is thread safe and easy to use.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am doing a simple coin flipping experiment for class that involves flipping a
We're building an app, our first using Rails 3, and we're having to build

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.