Linux 当从不同进程访问fifo时,某些东西会阻塞线程
我有一个Perl脚本和Python脚本。Perl脚本通过FIFO为Python脚本提供数据。Python脚本执行Perl脚本,并运行一些线程来处理Perl输出的数据。我将FIFO直接读入数据帧 有时会跑,有时会卡住。也许是死锁。我做错了什么 Python代码:Linux 当从不同进程访问fifo时,某些东西会阻塞线程,linux,pandas,perl,python-multithreading,multiprocess,Linux,Pandas,Perl,Python Multithreading,Multiprocess,我有一个Perl脚本和Python脚本。Perl脚本通过FIFO为Python脚本提供数据。Python脚本执行Perl脚本,并运行一些线程来处理Perl输出的数据。我将FIFO直接读入数据帧 有时会跑,有时会卡住。也许是死锁。我做错了什么 Python代码: from threading import Thread import os import subprocess import numpy as np import pandas as pd class MyThread(Thread)
from threading import Thread
import os
import subprocess
import numpy as np
import pandas as pd
class MyThread(Thread):
def __init__(self, fifo_name):
self.fifo_name = fifo_name
self.results = None
super(MyThread, self).__init__()
def run(self):
try:
os.mkfifo(self.fifo_name)
except FileExistsError:
pass
self.results = pd.read_csv(self.fifo_name)
def Main():
t11= MyThread("fifo1_msg1")
t11.start()
t12= MyThread("fifo1_msg2")
t12.start()
t21= MyThread("fifo2_msg1")
t21.start()
t22= MyThread("fifo2_msg2")
t22.start()
# run perl scripts that writes to fifos
subprocess.Popen(["perl", "dumpData.pl", file1, fifo1_msg1, fifo1_msg2, bufsize=1, stderr=subprocess.STDOUT)
subprocess.Popen(["perl", "dumpData.pl", file2, fifo2_msg1, fifo2_msg2], bufsize=1, stderr=subprocess.STDOUT)
t11.join()
t12.join()
t21.join()
t22.join()
t_plot = Thread(target=make_plots, args=(t11.results, t12.results, t21.results, t22.results))
t_plot.start()
t_plot.join()
if __name__ == "__main__":
Main()
Perl代码:
use warnings;
use strict;
$|++; # turn on autoflush
sub dump_file
{
my $filename = shift;
my $fifo_msg1 = shift;
my $fifo_msg2 = shift;
open(my $fifo1, ">", $fifo_msg1 ) || die();
open(my $fifo2, ">", $fifo_msg2) || die();
print $fifo1 "date,t,p\n";
print $fifo2 "time,x,y,z\n";
# some pseudo code as I don't want to put whole code
# while not eof, read line..
# read variables from line
if(case_bla) {
printf $fifo1 "%d,%d,%d\n", date,t,p;
}
else {
printf $fifo2 "%d,%d,%d,%d\n", time,x,y,z;
}
# end while
close($fifo1);
close($fifo2);
}
dump_file($inputfile, $fifo_msg1, $fifo_msg2);
首先,您没有自动刷新Perl输出流,因此使用者(Python脚本)在有足够(8K?4K?16K?)字节填充输出缓冲区之前不会接收任何内容。@mob,在5.14之前,Perl读取4 KiB块。从5.14开始,在构建perl时,块的大小是可配置的,默认值为8kib将禁用句柄上的缓冲。打开$fifo2时,您正在打开
$fifo_msg
,这应该是未定义的。您的意思可能是$fifo\u msg2
。总是把使用严格;使用警告代码>在perl脚本的开头。不,它不是。它在当前选择的输出句柄上启用autoflush
,该句柄为STDOUT
,除非您已更改它。打开后需要添加$fifo1->autoflush
和$fifo2->autoflush
。请将你的代码复制粘贴到问题中:输入内容并希望它足够接近,将导致错误和误导性的答案。