Search code examples
unixtcpjulia

Background Julia TCP server


My use case

Create a Julia background process which I can call from another server. When I pass it some arguments, it does stuff, sends a response, then waits for the next request.

What I tried

If there's a more 'Julia' way of doing it, let me know, but I thought this would be a good use case for a TCP server like in the docs' example:

using Sockets

@async begin
       server = listen(2001)
       while true
           sock = accept(server)
           @async while isopen(sock)
               write(sock,readline(sock))
           end
       end
   end

...Which works fine in the REPL! but I want it to run in the background permanently so I don't need to keep a REPL open.

When I put the above in app.jl and run julia app.jl it obviously runs then quits instantly. What's the best way to keep it running, even after closing my terminal?

Edits

  • My client is an Elixir server.
  • I would prefer TCP over HTTP just for performance reasons, but I can be persuaded if it ends up being too much work.

Solution

  • I put several option in the comment. Now the simplest approach is to remove @async in front of the begin statement in your code and run (I assume Linux):

    nohup julia app.jl &
    

    Of course you can add 1> and 2> to redirect standard output and standard error, put it as a cron job etc.

    JuliaWebAPI + ZeroMQ will be however more robust for production systems and Distributed will be more convenient for computational solutions.