3

I'm using the threading module in Python and have read the tutorial here. My intended program flow is that I have 2 functions foo() and bar() and the following happens

1) foo() runs and collects some data stored in data then stops.

2) bar() then runs continuously using data.

3) After 2 minutes foo() runs again and updates data while bar() continues to run using the old version of data.

4) After the successful update bar() starts using the new version of data.

5) Loop back to (3)

Now I want to run these as separate threads and my issue lies in how does foo() tell bar() it has finished running and the new version of data is available?

I've read about Queue but couldn't see exactly how I would use it in this instance.

1
  • How does bar use the data? Does it keep using it over and over, or just once? If just once, then either the Queue or Pipe will work. Commented Mar 28, 2014 at 21:04

1 Answer 1

1

It depends on how bar consumes the data. If bar has some natural point where it can choose to use existing or new data, it can read a queue filled by foo.

def foo(bar_q):
    while True:
        data = get_some_data()
        # copy or deepcopy if foo will be updating data instead of fetching new
        bar_q.put(copy.copy(data))
        time.sleep(120)

def bar(bar_q):
    # wait for the first data
    data = bar_q.get()
    bar_q.task_done()
    while True:
        process_some_data(data)
        try:
            # get new data if available
            data = bar_q.get_nowait()
            bar_q.task_done()
        except Queue.Empty:
            pass
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, it took me a while to get my head around the threading but its working!

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.