python 3.x - multiprocessing with delay of second function -
i have function write data text file, second function pull data same text file , show graph. want start second function few seconds after first function started , running both till both completed. in way can live graph. code have written below starts 2 functions simultaneously second function cannot see text file. need bit of delay second function give time first function create text file.
however because second function (live_graph) needs not pull data text file parameters (ex. title of graph) 1st function not sure if correct way proceed, seems not possible "key" function; got "key not defined". maybe have write text file parameters?
from multiprocessing import process import time def writing(): numentries = 0 text in get_all(newlista, "sentence", "text"): if text.lower().startswith( key.lower().split(none, 1)[0] ): pass elif len(text) > 500: pass elif len(text) < 80: pass else: on_data(text) numentries += 1 def live_graph(): #pull data text.txt time.sleep(5) if __name__=='__main__': p1 = process(target = writing) p1.start() p2 = process(target = live_graph) p2.start()
you want use multiprocessing.queue
for example (from docs):
from multiprocessing import process, queue def f(q): q.put([42, none, 'hello']) if __name__ == '__main__': q = queue() p = process(target=f, args=(q,)) p.start() print q.get() # prints "[42, none, 'hello']" p.join()
you use in code this:
from multiprocessing import process, queue import time def writing(q): keep_running = true numentries = 0 key = 'something, assume' text in get_all(newlista, "sentence", "text"): # python lets nice comparisons this, # like. , make more # obvious want lines of length between # 80 , 500 characters if 80 < len(text) < 500: firstword = key.lower().split(none, 1)[0] if text.lower().startswith(firstword): # note: *essential* in `on_data(text)` # after writing file run `file.flush()` # or close file, otherwise data *may* # buffered , hence, missing, when go read # file on_data(text) q.put(keep_running) keep_running = false q.put(keep_running) def live_graph(q): keep_running = true while keep_running: keep_running = q.get() # graph updates here if __name__=='__main__': q = queue() p1 = process(target = writing, args=(q,)) p1.start() p2 = process(target = live_graph, args=(q,)) p2.start()
Comments
Post a Comment