I have a large array in C# that I would like to pass to Python for processing. Instead of sending the values of the array, I would like to send a pointer and have Python access it that way. Here is my C# code:
NamedPipeServerStream server = null;
BinaryReader br = null;
BinaryWriter bw = null;
Thread thread = new Thread(() =>
{
server = new NamedPipeServerStream("NPtest");
Console.WriteLine("Waiting for connection...");
server.WaitForConnection();
Console.WriteLine("Connected.");
br = new BinaryReader(server);
bw = new BinaryWriter(server);
})
{ IsBackground = true };
thread.Start();
Process process = new Process() { StartInfo = new ProcessStartInfo("python.exe", "script.py") { UseShellExecute = false } };
process.Start();
Thread.Sleep(1000);
byte[] bytes = new byte[] { 32, 164, 9 };
IntPtr pointer = Marshal.AllocHGlobal(bytes.Length);
Marshal.Copy(bytes, 0, pointer, bytes.Length);
bw.Write(bytes.Length);
bw.Write(pointer.ToInt64());
Console.ReadKey(true);
Marshal.FreeHGlobal(pointer);
server.Close();
server.Dispose();
And here is script.py:
import struct
import ctypes
import numpy
f = open(r'\\.\pipe\NPtest', 'r+b', 0)
length = struct.unpack('i', f.read(4))[0]
f.seek(0)
print(length)
pointer = struct.unpack('q', f.read(8))[0]
f.seek(0)
print(hex(pointer))
data = numpy.ctypeslib.as_array(ctypes.cast(pointer, ctypes.POINTER(ctypes.c_ubyte * length)), shape=(length, ))
print(data)
When I get to the second to last line of the Python, it crashes without an error. I'm guessing it has to do with Python not being able to access that memory. Is there any way around this? Or is there some other architecture where I could pass a large array from C# to Python so processing can be performed?