MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Running the kernel as a daemon?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg61884] Re: Running the kernel as a daemon?
  • From: "ragfield" <ragfield at gmail.com>
  • Date: Thu, 3 Nov 2005 04:58:54 -0500 (EST)
  • References: <dka0fc$70v$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

> Can I start a program - say a QApplication - which has its own "event loop",
> not use MLMain,

Yes.

> establish a connection to a running Mathematica kernel,

This is a little tricky.  The only way (of which I'm aware) to
establish a connection to an already running kernel is for that kernel
to have created a link with LinkCreate[LinkMode -> "Listen"] and the
kernel must be blocked, waiting for the connection inside
LinkConnect[].  One can do this manually without too much difficulty,
but automating the process would be a challenge.  The Mathematica front
end doesn't even do that.

> and
> communicate with the kernel asynchronously.  What I mean is that I want to
> be able to run my program and do other things while the connection is
> opened, and waiting for my program to send commands.  I'm pretty sure
> I /can/ do this, but I'm not sure how easy it will be.

Yes, but it's non-trivial to do correctly.

> Do I need multiple threads in my program to do this?

No, this can be done on a single thread.  On Unix platforms, you can
get a file descriptor associated with the MathLink using
MLDeviceInformation() with UNIXPIPE_TYPE/PIPE_FD,
UNIXSOCKET_TYPE/SOCKET_FD, or SOCKET2_TYPE/SOCKET_FD.  With the file
descriptor you can wait for activity on the MathLink with select() or
some wrapper like Qt's QSocketNotifier, then call MLReady() to
determine whether a full packet is available.

> Also, I want to the kernel instance to run as a "server" or daemon, in the
> same way it does for the front end.

You may be a little confused about this point.  The front end does not
connect to an already running kernel daemon.  Under normal
circumstances the front end starts a new kernel instance when
computations need to be done and stops it when the front end quits.  It
does not connect to some already running kernel (unless the user
manually specifies the proper arguments to do so in the kernel
configuration dialog, and the kernel is waiting for the connection).

> Does MathLink make that easy?

Starting a new kernel instance is fairly easy.  Connecting to a running
kernel instance is more of a challenge.

> Is there a C++ wrapper for MathLink?

Not from Wolfram Research.

-Rob (Wolfram Research)


  • Prev by Date: Re: pattern matching question
  • Next by Date: Re: Running the kernel as a daemon?
  • Previous by thread: Running the kernel as a daemon?
  • Next by thread: Re: Running the kernel as a daemon?