Perform C # processing after executing Python processing asynchronously in Unity

Thing you want to do

I want to execute Python processing → C # processing in Unity

Why I wrote this article

If you want to run a chat system written in Python with Unity, a process called Process in C # is required. However, if you do heavy processing synchronously in Process, Unity will freeze! When asynchronous processing is performed in Process, C # processing cannot be executed after Python processing!

Realization method

//Variable to save the output data
private StringBuilder output = new StringBuilder();

public void Python()
    {   
        //Launch a new process
        var p = new Process();
        //Process settings
        p.StartInfo.FileName = pyExePath; //Python file location
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.CreateNoWindow = true;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.Arguments = pyCodePath; //File name to execute
        p.StartInfo.StandardOutputEncoding = Encoding.GetEncoding("shift_jis"); //Shift the output result of Python-Convert to jis

        //Event handler settings
        //Called every time there is output from python
        p.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
        {
            string str = e.Data;

            if (!String.IsNullOrEmpty(str))
            {
                UnityEngine.Debug.Log(str);
                output.Append(str+"\n");
            }

        });
        p.Start();

        UnityEngine.Debug.Log("Started asynchronously during process execution");


    }

    void Update()
    {
        if(output.Length != 0){
            UnityEngine.Debug.Log("Runs after the process is complete");
            output.Length = 0 ;
        }
    }
}

Brief commentary

If you write the following process in the function that performs Process, it will not work, so by taking the method of executing it with the Update function when the output result from Python obtained by Process is obtained, it was possible to make Python → C #.

Recommended Posts

Perform C # processing after executing Python processing asynchronously in Unity
File processing in Python
Multithreaded processing in python
Next Python in C
Text processing in Python
Queue processing in Python
C API in Python 3
[Unity (C #), Python] Try running Python code in Unity using IronPython
Extend python in C ++ (Boost.NumPy)
UTF8 text processing in python
Binary search in Python / C ++
Image Processing Collection in Python
Using Python mode in Processing
Signal processing in Python (1): Fourier transform
Perform Scala-like collection operations in Python
100 Language Processing Knock Chapter 1 in Python
ABC166 in Python A ~ C problem
Solve ABC036 A ~ C in Python
How to wrap C in Python
Solve ABC037 A ~ C in Python
Write C unit tests in Python
Solve ABC175 A, B, C in Python
Algorithm in Python (ABC 146 C Binary Search
Implement FIR filters in Python and C
Write O_SYNC file in C and Python
Easy image processing in Python with Pillow
Generate C language from S-expressions in Python
Duplicate prohibition processing in GAE / Python Datastore
Run Python in C ++ on Visual Studio 2017
Status of each Python processing system in 2020
Asynchronous processing in Python: asyncio reverse lookup reference
How to use the C library in Python
Output formatted output in Python, such as C / C ++ printf.
How to generate permutations in Python and C ++
Type Python scripts to run in QGIS Processing
View the result of geometry processing in Python
Run Python YOLOv3 in C ++ on Visual Studio 2017
Calculation result after the decimal point in Python
I investigated in detail about variable processing in python
A clever way to time processing in Python
Note on encoding when LANG = C in Python
Perform entity analysis using spaCy / GiNZA in Python
Boost.NumPy Tutorial for Extending Python in C ++ (Practice)
Python: Deep Learning in Natural Language Processing: Basics
Let's make some notification processing samples in Python
Call a Python script from Embedded Python in C ++ / C ++
Y / n processing in bash, python and Go
I tried adding a Python3 module in C
Parallel processing with no deep meaning in Python
[python] [c ++] bisect (* _bound) in reverse (descending) list
About list processing (Python beginners after learning Ruby)
First deep learning in C #-Imitating implementation in Python-
[Natural language processing / NLP] How to easily perform back translation by machine translation in Python