Python Forum
Avoid output buffering when redirecting large data (40KB) to another process
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Avoid output buffering when redirecting large data (40KB) to another process
#1
I have following python script, which is multi-threaded and a C# script from which I call python code. The data outputted from python script is not getting printed in C# code when data size is large (like 40KB). It's working fine for less than 100 bytes size. I am already using "-u" option while running python, to disable output buffering. But it's still buffering for large data (Some kind of block or line buffereing is happening, how to avoid this?)

How to get this work, please ., or any other alternate ways?


My python code:
import _thread
import time

# Define a function for the thread
def print_time( threadName, delay):
   count = 0
   while count < 5:
      time.sleep(delay)
      count += 1
      print ("%s: %s" % ( threadName, time.ctime(time.time()) ))

# Create two threads as follows
try:
   _thread.start_new_thread( print_time, ("T"*40000, 1, ) )
   _thread.start_new_thread( print_time, ("Thread-2", 0.5, ) )
except:
   print ("Error: unable to start thread")

x = input()
print (x)
My c# code:

using System;
using System.Collections;
using System.Diagnostics;
using UnityEngine;

public class MultiThreadCall : MonoBehaviour
{
    private ProcessStartInfo startInfo;
    private Process process;

    private void Start()
    {
        initialiseProcessStartInfo();
    }

    private void OnGUI()
    {
        if (GUI.Button(new Rect(5, 5, 50, 50), "START"))
        {
            if(process == null)
               run_cmd();
        }

        if (GUI.Button(new Rect(500, 5, 100, 50), "KILL"))
        {
            if (process != null)
               process.Kill();
        }

    }


    private void initialiseProcessStartInfo()
    {
        startInfo = new ProcessStartInfo();
        startInfo.FileName = "C:/Windows/py.exe";
        startInfo.Arguments = @"-u C:\Users\name\Desktop\multithreaded.py";
        startInfo.WorkingDirectory = @"C:\Users\name\Desktop\";

        startInfo.CreateNoWindow = true;
        startInfo.UseShellExecute = false;
        startInfo.RedirectStandardOutput = true;
        startInfo.RedirectStandardError = true;
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;
    }

    private void run_cmd()
    {
        process = new Process();
        process.StartInfo = startInfo;
        process.EnableRaisingEvents = true;
        process.OutputDataReceived += new DataReceivedEventHandler(OnDataReceived);
        process.ErrorDataReceived += new DataReceivedEventHandler(OnErrorReceived);

        process.Start();
        process.BeginOutputReadLine();
        process.BeginErrorReadLine();


    }

    private void OnDestroy()
    {
        if (process != null)
            process.Kill();
    }

    private void OnDataReceived(object sender, DataReceivedEventArgs e)
    {
        if (e.Data != null && e.Data.Length > 0)
        {
            UnityEngine.Debug.Log(e.Data + Environment.NewLine);
        }
    }

    private void OnErrorReceived(object sender, DataReceivedEventArgs e)
    {
        if (e.Data != null && e.Data.Length > 0)
        {
            UnityEngine.Debug.Log(e.Data + Environment.NewLine);
        }
    }

}
Reply
#2
You could perhaps add calls to sys.stdout.flush() in the code.

It may not be directly related to the issue, but I've always been told to use module threading instead of the low level _thread module. It would probably be a good idea to use this in your code.
Reply
#3
Thankyou for your reply. I have even tried what you said, but the issue still remains. It's working fine for data less than a KB. If data is something like 40 KB, it's getting buffered somewhere else !!
Reply
#4
you are creating an item of 40,000 'T's here,
"T"*40000
Is that your intention?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How should I process this data? SuchUmami 2 375 Jan-13-2024, 05:30 PM
Last Post: RockBlok
  How would I process this data? SuchUmami 2 557 Nov-12-2023, 10:05 AM
Last Post: SuchUmami
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,089 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Best way to process large/complex XML/schema ? MDRI 7 6,281 May-16-2021, 09:31 PM
Last Post: snippsat
  Code should download and output trading data tayy 2 2,636 Mar-04-2021, 04:07 AM
Last Post: tayy
  Looping Through Large Data Sets JoeDainton123 10 4,362 Oct-18-2020, 02:58 PM
Last Post: buran
  Extract data from large string pzig98 1 2,124 Jul-20-2020, 12:39 AM
Last Post: Larz60+
  Process Data from one csv file and write to another CSV file specific column ajin9581 1 1,974 Jun-17-2020, 06:00 PM
Last Post: buran
  Moving large amount of data between MySql and Sql Server using Python ste80adr 4 3,392 Apr-24-2020, 01:24 PM
Last Post: Jeff900
  alternative to nested loops for large data set JonnyEnglish 2 2,564 Feb-19-2020, 11:26 PM
Last Post: JonnyEnglish

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020