1

I am currently working with python v.2.7 on windows 8.

My programme is using threads. The threads execute a method named as getData() for infinite time that does the following:

  • makes the current thread to sleep for some time
  • calls the compareValues()
  • retrieve the information from the compareValues() and adds them to a list called myList

The compareValues() does the following:

  • generates a random number
  • checks if it is less than 5 or if it is greater than or equal to 5 and yields the result along with the current thread's name

I save the results of these threads to a list named as myList and then finally print this myList.

Problem: As the getData() is looping for infinite time. How can I access the myList for retrieving the results? What would be a good approach in this case. If you remove the while True: then the programm works fine.

Code:

import time
from random import randrange
import threading

myList = []
def getData(i):
   while True:
        print "Sleep for %d"%i
        time.sleep(i)
        data = compareValues()
        for d in list(data):
            myList.append(d)

def compareValues():
        number = randrange(10)
        name = threading.current_thread().name
        if number >= 5:
             yield "%s: Greater than or equal to 5: %d  "%(name, number)
        else:
             yield "%s: Less than 5: %d  "%(name, number)

threadList = []
wait = randrange(10)+1
t = threading.Thread(name = 'First-Thread', target = getData, args=(wait,))
threadList.append(t)
t.start()
wait = randrange(3)+1
t = threading.Thread(name = 'Second-Thread', target = getData, args=(wait,))
threadList.append(t)
t.start()
for t in threadList:
    t.join()
print "The final list"
print myList

Thank you for your time.

2
  • 1
    Why do you want the getData() method to loop infinitely? Commented Mar 10, 2014 at 8:34
  • 2
    This is an example of my real world problem. In my real world problem I have to read some different values from a server and update my old values respectively after every 5 seconds or so. So, I want to read values from the server infinitely. I just tried to convert my real world problem to this dummy problem. :) Commented Mar 10, 2014 at 8:43

1 Answer 1

1

I see a couple of inconsistencies that may help to make the code look a little bit more clear. For instance:

  1. compareValues() should return 'The result in string Format......' and not yield. Am I right? Because this function is going to evaluate only one value at a time.
  2. getData() should look like:

    def getData(i):
        while True:
            print "Sleep for %d"%i
            time.sleep(i)
            myList.append(compareValues())
    

Exactly for the same reason as I said in the previous point.

P.S.: I don't know why the code is not formatting properly. Sorry... :-(

I hope it helps!

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

2 Comments

Yes, your suggestions provided some hints that assisted me to solve my problem by modifying my code.
Wonderful! I'm happy about that.

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.