python - How to streamline this script -
i have script , work has 2 separate processes spawn listener threads kill process when kill sent listener via pipe.
multiprocessing import process, pipe threading import thread import time subalive = true testalive = true def sub_listener(conn): #listens kill main global subalive while true: data = conn.recv() if data == "kill": subalive = false #value kill break def test_listener(conn): #listens kill main global testalive while true: data = conn.recv() if data == "kill": testalive = false #value kill break def subprocess(conn, threadnum): t = thread(target=sub_listener, args=(conn,)) count = 0 threadval = threadnum t.start() while subalive: print "thread %d run number = %d" % (threadval, count) count = count + 1 def testprocess(conn, threadnum): t = thread(target=test_listener, args=(conn,)) count = 0 threadval = threadnum t.start() while testalive: print "this different thread %d run = %d" % (threadval, count) count = count + 1 sub_parent, sub_child = pipe() test_parent, test_child = pipe() runnum = int(raw_input("enter number: ")) threadnum = int(raw_input("enter number of threads: ")) print "starting threads" in range(threadnum): p = process(target=subprocess, args=(sub_child, i)) p.start() print "subprocess started" in range(threadnum): p2 = process(target=testprocess, args=(test_child, i)) p2.start() print "testproccess started" print "starting run" time.sleep(runnum) print "terminating subprocess run" in range(threadnum): sub_parent.send("kill") #sends kill listener print "terminating testprocess run" in range(threadnum): test_parent.send("kill") #sends kill listener p.join() p2.join()
id not need separate listener function hard coded every process call. thinking passing global variables when thread spawned. global variables differences between listener functions. guys!
you can access globals through globals()
dictionary.
>>> foo = 'value' >>> def change(name): ... globals()[name] = 'changed' ... >>> change('foo') >>> foo 'changed'
but suggest:
alive = {} def sub_listener(conn, key): #listens kill main while true: data = conn.recv() if data == "kill": alive[key] = false #value kill break
e.g.
from multiprocessing import process, pipe threading import thread import time alive = { 'sub': 1, 'test': 1, } def listener_factory(key): def listener(conn): #listens kill main while true: data = conn.recv() if data == "kill": alive[key] = false #value kill break return listener def process_factory(key): listener = listener_factory(key) def subprocess(conn, threadnum): t = thread(target=listener, args=(conn,)) count = 0 threadval = threadnum t.start() while alive[key]: print "thread[%s] %d run number = %d" % (key, threadval, count) count = count + 1 return subprocess def main(): sub_parent, sub_child = pipe() test_parent, test_child = pipe() runnum = int(raw_input("enter number: ")) threadnum = int(raw_input("enter number of threads: ")) print "starting threads" in range(threadnum): p = process(target=process_factory('sub'), args=(sub_child, i)) p.start() print "subprocess started" in range(threadnum): p2 = process(target=process_factory('test'), args=(test_child, i)) p2.start() print "testproccess started" print "starting run" time.sleep(runnum) print "terminating subprocess run" in range(threadnum): sub_parent.send("kill") #sends kill listener print "terminating testprocess run" in range(threadnum): test_parent.send("kill") #sends kill listener p.join() p2.join() if __name__ == '__main__': main()
Comments
Post a Comment