Multiprocessing With Delay Of Second Function
I have a function that write data to a text file, a second function that pull data from the same text file and show a graph. I want to start the second function a few seconds after
Solution 1:
You want to use the multiprocessing.Queue
For example (from the 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 could use that in your code like this:
from multiprocessing import Process, Queue
import time
defwriting(q):
keep_running = True
numentries = 0
key = 'something, I assume'for text in get_all(newlista, "sentence", "text"):
# Python lets you do nice comparisons like this,# which do what they look like. And make it more# obvious that you only want lines of length between# 80 and 500 charactersif80 < len(text) < 500:
firstword = key.lower().split(None, 1)[0]
if text.lower().startswith(firstword):
# Note: it is *essential* that in `on_data(text)`# after writing to your file you run `file.flush()`# or close the file, otherwise the data *may* be# buffered and hence, missing, when you go to read# the file
on_data(text)
q.put(keep_running)
keep_running = False
q.put(keep_running)
deflive_graph(q):
keep_running = Truewhile keep_running:
keep_running = q.get()
# do the graph updates hereif __name__=='__main__':
q = Queue()
p1 = Process(target = writing, args=(q,))
p1.start()
p2 = Process(target = live_graph, args=(q,))
p2.start()
Post a Comment for "Multiprocessing With Delay Of Second Function"