multithreading - Python: Update Local Variable in a Parallel Process from Parent Program -


i relatively new programming, , asking might task not possible. want start parallel process, continuously run until requested stop user. once process has started, update 1 of local variables inside parallel process parent program without stopping process. simple example, parallel process execute following:

import time  def loop(i):     while 1:         print         i+=1         time.sleep(1) 

which continuously iterates , updates i. clarity parent program contain:

from multiprocessing import process loop import loop  = 1 p = process(target = loop, args = (i)) p.start() 

from parent program, once "loop" has initiated, able change input "i" number , have loop continue iterate given next starting value. if has ideas on how this, appreciated.

you can use queue pass data between processes:

from multiprocessing import process, queue loop import loop  = 1 q = queue() q.put(i) p = process(target=loop, args=(q, )) p.start() 

whenever want transmit new value of other process, put in queue.

change loop.py module accordingly:

def loop(q):   while true:     = q.get()     print 

in main code, can put new values process:

while true:   i+=1   q.put(i)   time.sleep(1) 

Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -