问题描述
我有一个将数据写入文本文件的功能,第二个功能是从同一文本文件中提取数据并显示图形.我想在第一个功能启动后数秒内启动第二个功能,并同时运行两个功能,直到两个功能都完成为止.通过这种方式,我可以获得实时图表.我在下面编写的代码同时启动了两个功能,但是第二个功能看不到任何文本文件.对于第二个函数,我需要一点时间来给第一个函数创建文本文件的时间.
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 the first function is started and running both together till both completed. In this way I can get a live graph. The code I have written below starts the two functions simultaneously but the second function cannot see any text file. I need a bit of delay for the second function to give the time the first function to create the text file.
但是,因为第二个函数(live_graph)不仅需要从文本文件中提取数据,而且还需要从第一个函数中获取一些参数(例如图的标题),所以我不确定这是否是正确的处理方法,似乎不可能从另一个功能中获得钥匙";我得到密钥未定义".也许我还必须将参数也写入文本文件?
However because the second function (live_graph) needs not just pull data from the text file but also get some parameters (ex. title of the graph) from the 1st function I am not sure if this is the correct way to proceed, as it seems is not possible get a "key" from another function; I got "key not defined". Maybe I have to write to the text file also the parameters?
from multiprocessing import Process
import time
def writing():
numentries = 0
for 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 from text.txt
time.sleep(5)
if __name__=='__main__':
p1 = Process(target = writing)
p1.start()
p2 = Process(target = live_graph)
p2.start()
推荐答案
例如(来自文档):
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
def writing(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 characters
if 80 < 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)
def live_graph(q):
keep_running = True
while keep_running:
keep_running = q.get()
# do the 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()
这篇关于具有第二功能延迟的多重处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!