Named Pipes between Julia and CSharp

Hi dear community :slight_smile:

I’m trying to communicate between Julia and CSharp with named pipes on Windows.

I’m using following code in Csharp server:

using (var pipe = new NamedPipeServerStream("cs", PipeDirection.InOut))
{
    pipe.WaitForConnection();
    var sw = new StreamWriter(pipe);
    var sr = new StreamReader(pipe);

    while (true)
    {
        var s = sr.ReadLine();
        sw.WriteLine(s);
        sw.Flush();
    }
}

and the following in Julia client:

using Sockets
c = connect(raw"\\.\pipe\cs")
function write_read(c, str)
    write(c, str * "\n")
    readline(c)
    nothing
end
data = rand('a':'z', 10_000_000) |> String
@time write_read(c, data) # about 27s

Julia-Julia, CSharp-CSharp and Julia(Server)-Sharp(Client) are much faster (0.06s).

How can I speed it up. In other words, what is the problem?

Any idea what it could be?

I’m not sure write sends a final newline, which your code seems to expect.

1 Like

Sorry, that was a typo. Did you manage to replicate it on your machine?

I don’t have a CSharp setup so I can’t really test anything. What I would suggest is printing the current time on the CSharp server when the line has been read, when the line has been written, and after the channel has been flushed. On the Julia side print the current time after the write and after the readline.

Hopefully that would suggest where the issue might be. Also on the Julia side, can you do a flush(c) after the write?

I am currently working on a quite similar project, but my server is implemented on julia’s side, and this communication felt pretty fast.

However, that is why I am communicating on this thread instead of a new one, I was wondering how you handle the two way communication between your processes.

The current state of my program is the following :

  1. C# fires up a julia process which takes a small file starting my named pipe server using the Sockets library and a

    • listen call to start the named pipe server,
    • accept call to create a socket from which I can read on the pipe.

    At that point my julia server waits for a message to come through the pipe .

  2. I have a function based on the MessagePack-CSharp package which handles the serialization on the C# side

  3. I send data (as MessagePackObjects) or instructions (as strings) over the pipe.

Now I was wondering what the best way to send information back to C# was :

  • Communicate over the same pipe ? That means knowing when to wait and when to send information, and this works only with named pipes (but I am unsure if julia supports this)
  • Create another pipe only for the return communication?

As you seem to have tested in details the communication over pipes using julia, I eager to have your opinion on this. Cheers !