Python—如何与其他脚本共享实时数据
我试图找到一种最简单的方法来将实时变量从一个脚本共享到另一个脚本。第一个脚本将读取seonsor数据,另一个脚本将根据第一个脚本中的实时数据进行计算。我想单独运行它们。我希望能够杀死第二个脚本,并再次运行它没有任何问题 我想有第二个脚本打印实时数据时,它是启动Python—如何与其他脚本共享实时数据,python,multithreading,real-time-data,Python,Multithreading,Real Time Data,我试图找到一种最简单的方法来将实时变量从一个脚本共享到另一个脚本。第一个脚本将读取seonsor数据,另一个脚本将根据第一个脚本中的实时数据进行计算。我想单独运行它们。我希望能够杀死第二个脚本,并再次运行它没有任何问题 我想有第二个脚本打印实时数据时,它是启动 更新: 我终于有时间玩os.pipe()。我已经设法运行了一些使用os.fork()的脚本,但当我试图将一个脚本拆分为两个单独的程序时,我开始遇到一些问题 我启动并正在运行的程序: #!/usr/bin/python
更新: 我终于有时间玩os.pipe()。我已经设法运行了一些使用os.fork()的脚本,但当我试图将一个脚本拆分为两个单独的程序时,我开始遇到一些问题 我启动并正在运行的程序:
#!/usr/bin/python
import os, sys
r, w = os.pipe()
processid = os.fork()
if processid:
os.close(w)
r = os.fdopen(r)
print("Parent reading")
str = r.read()
print("text =", str)
sys.exit(0)
else:
os.close(r)
w = os.fdopen(w, 'w')
print("Child writing")
w.write("Text written by child...")
w.close()
print("Child closing")
sys.exit(0)
基于这个脚本,我试图编写自己的独立脚本
打印管道时间的第一个脚本:
#!/usr/bin/python
import os, sys, time
stdout = sys.stdout.fileno()
r, w = os.pipe()
#os.close(r)
w = os.fdopen(w, 'w')
i = 0
while i < 1000:
i = i + 1
w.write('i' + " ")
time.sleep(1)
当我尝试运行脚本时,什么都没有发生。有什么建议我做错了什么?也许我遗漏了一些关于标准输入和输出以及os.pipe()的细节?编辑::只是为了说明一个可能的毒丸方法的粗略示例。不幸的是,python的(多处理)队列没有实现
peek
调用,因此我使用了第二个队列:
作者:
import time
import sys
def write(queue_msg, queue_term):
i = 0
sys.stdout = open("writer" + ".out", "w")
sys.stderr = open("writer_err" + ".out", "w")
stop_writing = True
while i<1000:
if not(stop_writing):
print("Writer at iteration ", i)
queue_msg.put("Iteration " + str(i)) # put a value inside the queue
i += 1
time.sleep(1)
q_term_val = queue_term.get() if not(queue_term.empty()) else "CONTINUE" #Check that the queue is not empty otherwise put a non important value
print(q_term_val)
if q_term_val == "START":
print("Start working")
stop_writing = False
elif q_term_val == "STOP":
print("Stop working")
stop_writing = True
sys.stderr.flush() #flush stdout and stderr to disk
sys.stdout.flush()
主要内容:
您的文件夹中应该有
reader.out
文件(也叫writer.out),显示读卡器确实读取了写入器写入队列的值。在“writer.out”中,您可以看到写入器在收到毒药丸时停止写入队列。而读取器在时间之后。sleep(5)
调用将从正确的位置再次启动。最简单的机制是使用文件系统。将脚本1附加到文本文件。然后让script2轮询新行…或使用命名管道。对于文件或管道,请注意缓冲。我认为管道是我一直在寻找的东西。我现在需要学习它是如何工作的。谢谢或者您可以在第三个脚本中使用多线程/多处理和队列,并让这两个脚本在其中交互。是否有任何特定的多处理模块?@asettourf谢谢您的帮助。有一件事我错过了。它正在共享数据,但它处于队列中。如果我将读卡器脚本延迟到time.sleep(3),那么它将不会打印实时数据,而是打印排队的数据。@ukaszŻurek我不确定我是否理解您的评论,如果您想要实时,这将很难用Python和大多数操作系统实现。现在,如果您担心time.sleep
调用,您可以删除它们,脚本仍然有效,它们只是不在每个循环之间等待。我前面已经提到过。我想运行一个脚本来计算时间,运行第二个脚本来读取当前时间——即使我关闭并再次运行第二个脚本。您的示例将打印队列中的每个值。当我改变时间。睡眠(3)我期望得到时间:1,4,7,10@ŁukaszŻurek我编辑了我的帖子,以说明您的问题的可能解决方案,在需要时使用两个队列来同步每个工人之间的工作。没想到解决这样简单的问题会如此困难。此时,我试图运行您的脚本,但它冻结了,没有任何结果/输出。我明白你的意思了。今晚我将更深入地了解你的剧本。如果我们把毒丸和烟斗结合起来呢?只是为了触发写入管道?第二个脚本读取频率较低,所以应该可以吗?关于管道,我必须读取写入管道的所有数据吗?看来几个节目的交流是件大事。
import time
import sys
def write(queue_msg, queue_term):
i = 0
sys.stdout = open("writer" + ".out", "w")
sys.stderr = open("writer_err" + ".out", "w")
stop_writing = True
while i<1000:
if not(stop_writing):
print("Writer at iteration ", i)
queue_msg.put("Iteration " + str(i)) # put a value inside the queue
i += 1
time.sleep(1)
q_term_val = queue_term.get() if not(queue_term.empty()) else "CONTINUE" #Check that the queue is not empty otherwise put a non important value
print(q_term_val)
if q_term_val == "START":
print("Start working")
stop_writing = False
elif q_term_val == "STOP":
print("Stop working")
stop_writing = True
sys.stderr.flush() #flush stdout and stderr to disk
sys.stdout.flush()
import time
import sys
def read(queue_msg, queue_term):
sys.stdout = open("reader" + ".out", "w") #redirect process stdout to file "reader.out"
sys.stderr = open("reader_err" + ".out", "w")
queue_term.put("START")
while True:
value = queue_msg.get()
print(value) # write value to reader.out filer as mentioned above
if int(value.split(" ")[1]) > 10:
print("Stop working, I am tired...")
queue_term.put("STOP")
time.sleep(5) #wait 5 seconds before sending start to the worker
queue_term.put("START")
time.sleep(1)
sys.stderr.flush()
sys.stdout.flush()
import reader
import writer
import multiprocessing as mp
def main():
q_msg = mp.Queue()
q_term = mp.Queue()
r = mp.Process(target=reader.read, args=(q_msg, q_term,))
w = mp.Process(target=writer.write, args=(q_msg, q_term,))
r.start()
w.start()
print("Processes started, check that in your folder there are a reader.out and a writer.out files")
if __name__ == "__main__": # "workaround" needed on Windows
main()