python - cant reference a global dictionary entry -
i aware global variables not best way deal things in case fine doing. not going doing heavy read/writes reads.
alive = {'subalive': true, 'testalive': true}; def sub_listener(conn): #listens kill main global alive while true: data = conn.recv() if data == "kill": alive['subalive'] = false; #value kill break def subprocess(conn, threadnum): t = thread(target=sub_listener, args=(conn,)) count = 0 threadval = threadnum t.start() run = alive[subalive]; while run: print "thread %d run number = %d" % (threadval, count) count = count + 1 sub_parent, sub_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 "starting run" time.sleep(runnum) print "terminating subprocess run" in range(threadnum): sub_parent.send("kill") #sends kill listener p.join()
i error
nameerror: global name 'testalive' not defined traceback (most recent call last): file "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap self.run() file "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run self._target(*self._args, **self._kwargs) file "multiprocessdemo.py", line 38, in subprocess run = alive[subalive]; nameerror: global name 'subalive' not defined
i have tried accessing dictionary few different ways , can't seem find out wrong on google. if use separate variables work wont dynamically scale well.
put quotes around subalive
:
run = alive['subalive']
Comments
Post a Comment