1

I have been working on this bit of script and I am having issues sending a kill message to a the control_listener nor does it seem to get kill from the kill_timer thread

from multiprocessing import Process, Pipe
from threading import Thread
import time


alive = {'subAlive': True, 'testAlive': True};

def kill_timer(conn, time):
    time.sleep(time) 
    conn.send("kill")

def control_listener(conn, threadAlive, runTime): #listens for kill from main
    time_parent, time_child = Pipe() 
    t = Thread(target=kill_timer, args=(time_parent, runTime)) 
    global alive
    while True:
          data = conn.recv()
          timer = time_child.recv()
          if data == "kill" | timer == "kill":
             print "Killing"
             alive[threadAlive] = False; #value for kill
             print "testListner alive %s" % threadAlive, alive[threadAlive];
             break

def subprocess(conn, threadNum, threadAlive, runTime):
    t = Thread(target=control_listener, args=(conn, threadAlive, runTime))
    count = 0
    threadVal = threadNum 
    t.start()
    run = alive['subAlive'];
    while run == True:
          print "Thread %d Run number = %d" % (threadVal, count)
          count = count + 1
          run = alive['subAlive']; 


def testprocess(conn, threadNum, threadAlivei, runTime):
    t = Thread(target=control_listener, args=(conn, threadAlive, runTime))
    count = 0
    threadVal = threadNum 
    t.start()
    run = alive['testAlive'];
    while run == True:
          print "This is a different thread %d Run = %d" % (threadVal, count)
          count = count + 1
          run = alive['testAlive'];

sub_parent, sub_child = Pipe()
test_parent, test_child = Pipe()
runNum = int(raw_input("Enter a number: ")) 
threadNum = int(raw_input("Enter number of threads: "))
runTime = int(raw_input("Enter a timeout number: "))


print "Starting threads"

for i in range(threadNum):
    p = Process(target=subprocess, args=(sub_child, i, 'subAlive', runTime))
    p.start()

print "Subprocess started"

for i in range(threadNum): 
    p2 = Process(target=testprocess, args=(test_child, i, 'testAlive', runTime))
    p2.start()

print "Testproccess started"

print "Starting run"

time.sleep(runNum) 

print "Terminating Subprocess run"
for i in range(threadNum):
    sub_parent.send("kill") #sends kill to listener
    print "Testprocess termination alive", alive['subAlive'];

print "Terminating Testprocess run"
for i in range(threadNum):
    test_parent.send("kill") #sends kill to listener
    print "Testprocess termination alive", alive['subAlive'];

p.join()
p2.join()

For some reason I can't get the control_listener to trigger it seems to go into an unending loop.

4

0 回答 0