Что-то блокирует потоки при доступе к fifo из разных процессов - PullRequest
0 голосов
/ 12 июня 2018

У меня есть Perl-скрипт и Python-скрипт.Скрипт Perl питает скрипт Python с помощью FIFO.Сценарий Python выполняет сценарий Perl, а также запускает несколько потоков для обработки данных, выводимых из Perl.Я читаю FIFO непосредственно в pandas DataFrame.

Иногда он запускается, иногда зависает.Возможно, мертвый замок.Что я делаю не так?

Код Python:

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);
...