Writing Arrays to Binary Files Takes a Long Time in Python

I have an RFSoC4x2 board with Pynq 3 installed on it.
My application is a radar system in which I send pulses on the DAC channel and receive them on the ADC channel. In the Python program that I have written to control the radar, I want to write the received data from the FPGA into binary files on the SD card of the board. However, I have encountered a strange issue. The for loop responsible for writing the newly received data into the binary file sometimes takes much more time than usual. It seems that the program stops working for a couple of seconds.

Am I using the Pynq functions correctly, or can they be more optimized?
I have attached a picture that shows the time it took to write each received pulse on the SD card.

Look into the fourth line:
Screenshot from 2023-09-12 15-16-39 (copy)

Here is my code snippet:

        for i in range(int(self.pulseNumber)):

            self.dma0_recv.transfer(output_buffer0)
            self.dma1_recv.transfer(output_buffer1)
            self.dma2_recv.transfer(output_buffer2)
            self.dma3_recv.transfer(output_buffer3)
            self.dma0_recv.wait()
            self.dma1_recv.wait()
            self.dma2_recv.wait()
            self.dma3_recv.wait()

            self.data_source_mmio.write(0, 12)

            begin_writing = time.time()

            filename = self.experiment_name + "_waveform" + str(i).zfill(4) + "_channel0.bin"
            file_path = os.path.join(folder_name, filename)
            #np.save(file_path, output_buffer0)
            with open(file_path, 'wb') as file:
                file.write(output_buffer0.tobytes())

            filename = self.experiment_name + "_waveform" + str(i).zfill(4) + "_channel1.bin"
            file_path = os.path.join(folder_name, filename)
            #np.save(file_path, output_buffer1)
            with open(file_path, 'wb') as file:
                file.write(output_buffer1.tobytes())

            filename = self.experiment_name + "_waveform" + str(i).zfill(4) + "_channel2.bin"
            file_path = os.path.join(folder_name, filename)
            #np.save(file_path, output_buffer2)
            with open(file_path, 'wb') as file:
                file.write(output_buffer2.tobytes())

            filename = self.experiment_name + "_waveform" + str(i).zfill(4) + "_channel3.bin"
            file_path = os.path.join(folder_name, filename)
            #np.save(file_path, output_buffer3)
            with open(file_path, 'wb') as file:
                file.write(output_buffer3.tobytes())

            end_writing = time.time()
2 Likes