Python Threading - Simple but tricky
<p>Could someone please point out, why my code is not printing "hello". I feel that my thread 2 - t2 is not starting. This is just a code snippet, I am trying to work on to implement in my main program. Basically, my idea is the following: </p>

<ol>
<li>Have a single function - which has two set of codes ---- one code in "if condition statement" and the other in "while loop"</li>
<li>Thread 1 - uses function 1 - "while loop" continuously - until I abruptly stop the program</li>
<li>Thread 2 or thread 3 or thread 4 etc, wants to use the same function - but the code under "if condition statement"</li>
<li>My approach in the following code is to have a thread 1 - run continuously, while thread2 - does its work without interrupting thread1. </li>
</ol>

<p>The code:</p>

<pre><code>import threading
import Queue



def Continuous(stop_event, queue_read, queue_write,lock):

    #lock.acquire()
    try:

        if stop_event == True:

            print stop_event.is_set()
            print "hello"
            c = 7
            d = 8
            msg2 = (c+d)*2
            queue_write.put(msg2)

        while not stop_event.wait(1):
            print "hello2"
            #print ("working on %s" % arg)
            a = 3;
            b = 4
            msg1 = a*b
            queue_read.put(msg1)
            time.sleep(1)

    finally:
        print "Inside finally"
        #lock.release()

    print"Outside try and finally"




def main():

    pill2kill = threading.Event()
    lock = threading.Lock()

    pill2kill.clear()
    queue1 = Queue.Queue()
    queue2 = Queue.Queue()
    #self.queue2 = Queue.Queue()
    t1 = threading.Thread(target = Continuous, args = (pill2kill,queue1,queue2,lock))
    t1.deamon = True
    t1.start()

    print "asdfasfd"
    time.sleep(2)
    pill2kill_1 = threading.Event()
    pill2kill_1.set()
    print "hjgkhj"
    t2 = threading.Thread(target=Continuous, args=(pill2kill_1, queue1, queue2,lock))
    t2.start()
    t2.join()
    pill2kill.clear()
    print "End of program"


main()
</code></pre>

<p>PS: I am extremely sorry, for posting my code this way. I tried for over 15 minutes and I am unsuccesful to get it formatted</p>

<p>(Converted from comment because subsequent comment by OP made it clear what they were expecting) The problems come from misuse of <code>Event</code>:</p>

<ol>
<li><code>if stop_event == True:</code> will always evaluate to <code>False</code> and the <code>if</code> block never executes its contents (<code>stop_event</code> is an <code>Event</code>, it's not equal to anything of type <code>bool</code>, did you mean <code>if stop_event.is_set():</code>?). This prevents <code>hello</code> from being printed.</li>
<li>You set the event for the second thread before you create the thread, let alone start it, so <code>while not stop_event.wait(1):</code> will exit the loop immediately without executing the body (preventing <code>hello2</code> from being printed); the only expected output would be from the <code>finally</code> block and below the <code>finally</code>.</li>
</ol>

<p>Side-note: <code>t1.deamon = True</code> does nothing; the attribute is spelled <code>daemon</code>. Since you never <code>set</code> the <code>Event</code> for thread 1, this seems like it would mean the program would never exit.</p>

python multithreading