2

I am trying to run two threads, each with an argument passed to it to process. However, it seems that the threads are running sequentially, not in parallel. Witness:

$ cat threading-stackoverflow.py 
import threading

class CallSomebody (threading.Thread):
        def __init__(self, target, *args):
                self._target = target
                self._args = args
                threading.Thread.__init__(self)

        def run (self):
                self._target(*self._args)

def call (who):
        while True:
                print "Who you gonna call? %s" % (str(who))

a=CallSomebody(call, 'Ghostbusters!')
a.daemon=True
a.start()
a.join()

b=CallSomebody(call, 'The Exorcist!')
b.daemon=True
b.start()
b.join()

$ python threading-stackoverflow.py 
Who you gonna call? Ghostbusters!
Who you gonna call? Ghostbusters!
Who you gonna call? Ghostbusters!
Who you gonna call? Ghostbusters!
Who you gonna call? Ghostbusters!
Who you gonna call? Ghostbusters!
Who you gonna call? Ghostbusters!

I would expect to have some lines return Ghostbusters! and others return The Exorcist!, however the Ghostbusters! lines go on forever. What must be refactored to have each thread get some processor time?

0

1 Answer 1

3

this is your problem: calling a.join() before b.start()

you want something more like:

a=CallSomebody(call, 'Ghostbusters!')
a.daemon=True
b=CallSomebody(call, 'The Exorcist!')
b.daemon=True
a.start()
b.start()
a.join()
b.join()
Sign up to request clarification or add additional context in comments.

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.