I have a Perl script and Python script. Perl script feeds Python script by means of FIFO. Python script executes the Perl script and also runs some threads to process the data output from Perl. I read the FIFO directly into pandas DataFrame.
Sometimes it runs, sometimes it is stuck. Perhaps dead lock. What am I doing wrong?
Python code:
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 Code:
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);