2

I intend using threads/queues with python 2.5.2 But it seems that python becomes freezed at the queue.join()-command. The output of the followong code is only: BEFORE

import Queue
import threading

queue = Queue.Queue()

class ThreadUrl(threading.Thread):

    def __init__(self, queue):
        threading.Thread.__init__(self)
        self.queue = queue

    def run(self):
        while True:

            i = self.queue.get()
            print i
            self.queue.task_done()


def main():

    for i in range(5):
        t = ThreadUrl(queue)
        t.setDaemon(True)
        t.start()

    for i in range(5):
        queue.put(i)

    print "BEFORE"
    queue.join()
    print "AFTER"


main()

Has someone an idea about what is going wrong?

7
  • queue.put(1) ... not saying this is the solution. But just check the id(queue) is same across all functions ... if not then use global queue in function ... Commented Aug 16, 2011 at 7:48
  • this works just fine for me with Python 2.6.6 on Windows Commented Aug 16, 2011 at 7:48
  • This code looks pretty flawless, and I read this kinda stuff all day :\ Commented Aug 16, 2011 at 7:51
  • @Eli - I don't think that was a copy-paste error, I think that was his real error. Commented Aug 16, 2011 at 7:52
  • @agf: hmm, if that's so, good catch :) Commented Aug 16, 2011 at 7:53

4 Answers 4

1

I think it is the t.setDaemon(True) part.

So in Python > 2.6, use:

t.setDaemon(True)

And in Python < 2.6, use:

t.daemon = True
Sign up to request clarification or add additional context in comments.

Comments

0

You run() method on your ThreadUrl class is indented too far. The thread is never started as a result. If you put the indention of the run method at the same indentation level as init() it'll work fine.

4 Comments

Thanks for the hint. But now after calling the queue.put(i)-command the first (or sometimes a second) time python crashes.... !?
Crashes how? A standard exception or it dies with a signal (SEGV)?
IDLE freezes and then the window becomes closed by the task manager.
There might be issues with threading in old versions of IDLE. I think it is resolved in newer versions.
0

The solution I now found is:

Don't use Python 2.5.2! If one uses Python 2.7.2 instead the code above works very well.

Thank you all!

Comments

0

Use Daemon=True. That will ensure that your thread exits once the main function is executed.

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.