0

I have some python application with 2 threads. Each thread operates within a separate gui. The GUIs need to operate independently without blocking. I am trying to figure out how to make thread_1 trigger an event to happen in thread_2?

Below is some code I want function foo to trigger function bar in the simplest, most elegant way as quickly as possible, without consuming unnecessary resources. Below is what I've come up with.

bar_trigger=False  #global trigger for function bar.
lock = threading.Lock()

class Thread_2(threading.Thread):
  def run(self):
    global lock, bar_trigger
    while(True):
       lock.acquire()
       if bar_trigger==True:
         Thread_2.bar()  #function I want to happen 
         bar_trigger=False
       lock.release()
       time.sleep(100) #sleep to preserve resources
                       #would like to preserve as much resources as possible 
                       # and sleep as little as possible.

  def bar(self):
       print "Bar!"

class Thread_1(threading.Thread):
  def foo(self):
      global lock, bar_trigger
       lock.acquire()
       bar_trigger=True  #trigger for bar in thread2
       lock.release()

Is there a better way to accomplish this? I'm not a threadding expert so any advice on how to best trigger a method in thread_2 from within thread_1 is appreciated.

1
  • 1
    I don't know of any GUI frameworks that support GUI programming in multiple threads. Which framework are you using? Commented Mar 15, 2011 at 23:00

3 Answers 3

1

Without knowing what you're doing and what GUI framework you're using, I can't get into much more detail, but from your problem's code snippet, it sounds like you're looking for something called conditional variables.

Python comes with them included by default in the threading module, under threading.Condition You might be interested in threading.Event as well.

Sign up to request clarification or add additional context in comments.

Comments

0

How are these threads instantiated? There should really be a main thread that oversees the workers. For example,

import time
import threading

class Worker(threading.Thread):
    def __init__(self, stopper):
        threading.Thread.__init__(self)
        self.stopper = stopper

    def run(self):
        while not self.stopper.is_set():
            print 'Hello from Worker!'
            time.sleep(1)

stop = threading.Event()
worker = Worker(stop)
worker.start()

# ...

stop.set()

Using a shared Event object is just one way of synchronizing and sending messages between threads. There are others, and their usages depend on the specifics.

Comments

0

One option would be to share a queue between the threads. Thread 1 would push an instruction into the queue and thread two would poll that queue. When Thread 2 sees the queue is non-empty, it reads off the first instruction in the queue and calls the appropriate function. This has the additional benefit of being fairly loosely couple which can make testing each thread in isolation easier.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.