python - Multiprocessing and global True/False variable -
i'm struggling head around multiprocessing , passing global true/false variable function.
after get_data() finishes want analysis() function start , process data, while fetch() continues running. how can make work? tia
import multiprocessing ready = false def fetch(): global ready get_data() ready = true return def analysis(): analyse_data() if __name__ == '__main__': p1 = multiprocessing.process(target=fetch) p2 = multiprocessing.process(target=analysis) p1.start() if ready: p2.start()
you should run 2 processes , use shared queue exchange information between them, such signaling completion of action in 1 of processes.
also, need have join() statement wait completion of processes spawn.
from multiprocessing import process, queue import time def get_data(q): #do data time.sleep(2) #put event in queue signal get_data has finished q.put('message get_data analyse_data') def analyse_data(q): #waiting get_data finish... msg = q.get() print msg #will print 'message get_data analyse_data' #get_data has finished if __name__ == '__main__': #create queue exchanging messages between processes q = queue() #create processes, , send shared queue them processes = [process(target=get_data,args(q,)),process(target=analyse_data,args=(q,))] #start processes p in processes: p.start() #wait until processes complete p in processes: p.join()
Comments
Post a Comment