#1797 Executing external process in fantom

Xan Tue 28 Feb 2012

Hi,

I want to know how can I execute an external program (in other language) in Fantom. Something like "ls" in unix systems

Thanks a lot, Xan.

brian Tue 28 Feb 2012

You need to use sys::Process

Xan Tue 28 Feb 2012

Yes, this is what I want.

Thanks, Brian.

Xan Tue 28 Feb 2012

Could it be call asyncroniously? I'm thinking in RPC? I did not find RPC services in fantom libraru.

Thanks, Xan.

brian Tue 28 Feb 2012

It just spawns a process - it doesn't block if that is what you are asking. However you typically have to handle the I/O somehow. Sometimes I spawn a Process in an Actor and have it deal with the I/O. BTW, its just a wrapper around Java's Process API.

Xan Tue 28 Feb 2012

I mean to call RPC server (in non-Fantom code) in Fantom client. Is it possible?

brian Tue 28 Feb 2012

Not unless you used some Java library with Java FFI

qualidafial Tue 28 Feb 2012

Be careful spawning processes from a java web server. Java uses the fork C function followed by exec to spawn processes on Linux. Due to the behavior of fork on Linux there is a small window of time in which the spawned process allocates the same amount of memory as the parent process. On very large web applications, this can easily consume all memory if too many processes are spawned at once.

dobesv Wed 29 Feb 2012

@qualidafial

http://linux.die.net/man/2/fork

Under Linux, fork() is implemented using copy-on-write pages, so the only penalty that > it incurs is the time and memory required to duplicate the parent's page tables, and to create a unique task structure for the child.

Maybe for systems with less advanced MMUs that don't support copy-on-write this would be a problem but otherwise you should be OK.

Xan Thu 1 Mar 2012

Not unless you used some Java library with Java FFI Ok. I will use it. For the other hand, it's a pain we have not native fantom RPC client.

brian Thu 1 Mar 2012

Not unless you used some Java library with Java FFI Ok. I will use it. For the other hand, it's a pain we have not native fantom RPC client.

When you say "RPC" what exactly do you mean?

Xan Mon 5 Mar 2012

RPC by me means this http://golang.org/pkg/rpc/ for example

brian Mon 5 Mar 2012

I can't tell from looking at that what protocol it is using - maybe JSON-RPC. If you want a specific protocol, then obviously you would need a Fantom implementation of it.

But if you are just looking for a simple Fantom specific RPC implementation, it is pretty trivial to roll your own using WebClient, WebMod, and serialization. For example here is a simple, but complete working client and server RPC implementation:

using web
using wisp

class Main
{
  static Void main()
  {
    WispService { port = 8080; root = Server()  }.start
    r := Client(`http://localhost:8080/`).call("sum", [4, 5])
    echo("c: $r")
  }
}

const class Client
{
  new make(Uri uri) { this.uri = uri.plusSlash }
  const Uri uri
  Obj? call(Str method, Obj? arg)
  {
    c := WebClient(uri+`$method`)
    argStr := StrBuf() { out.writeObj(arg) }.toStr
    c.postStr(argStr)
    if (c.resCode != 200) throw Err("Invalid response $c.resCode")
    resultStr := c.resIn.readAllStr
    return resultStr.in.readObj
  }
}

const class Server : WebMod
{
  override Void onPost()
  {
    method := typeof.method(req.modRel.path.first ?: "", false)
    if (method == null) { res.sendErr(404); return }
    arg := req.in.readObj
    echo("s: $method.name($arg)")
    result := method.call(this, arg)
    res.statusCode = 200
    res.headers["Content-Type"] = "text/plain"
    res.out.writeObj(result)
  }

  Int sum(Int[] nums) { sum := 0; nums.each |x| { sum+=x }; return sum }
}

Xan Tue 6 Mar 2012

Thank you very much, Brian, for this example. It's very illustrative

For the other hand, what protocol do you recommend to use?

  • custom http protocol?
  • rpc-json?
  • STOMP
  • ???

The "problem" is that in my project I could have multiple clients and servers: each programmed with different programming lang: go, d, fantomm ... so I need "standard" protocol for communication.

And I need this protocol were async.

Thanks for all, Xan.

brian Tue 6 Mar 2012

For the other hand, what protocol do you recommend to use?

IMO structuring clean URIs in your HTTP interface using a REST paradigm is way better than generic RPC toolkits.

When possible I find the most universally useful payload format is CSV if you have tabular data. If that doesn't work, I think JSON seems like the best bet these days.

Xan Tue 6 Mar 2012

Rest? Okay. I have to switch paradigm. Is it async? I will search to Java REST server and clients....

Thanks another way, Xan.

brian Tue 6 Mar 2012

Rest? Okay. I have to switch paradigm. Is it async? I will search to Java REST server and clients...

Well HTTP isn't really async, although some web servers use an async model to poll a socket until data is ready (Wisp doesn't do that).

Xan Wed 7 Mar 2012

So what is my alternative for async proto?

Thanks,

Login or Signup to reply.