Re: having standalone threads managed by netplex

Jake Donham <[email protected]> Wed, 4 Feb 2009 13:26:51 -0800
Newsgroups gmane.comp.lang.ocaml.lib.net.devel
Message-ID <[email protected]>
On Wed, Feb 4, 2009 at 12:34 PM, Anil Madhavapeddy <[email protected]> wrote:

> Is it possible in the Netplex framework to create these singleton
> threads which exist outside of the actual protocol reactors?

You can make a processor that doesn't bind a socket. E.g.

let factory () =
  let hooks =
    object
      inherit Netplex_kit.empty_processor_hooks() as super

      method post_start_hook c =
        super#post_start_hook c;
        (* do your work *)

      method shutdown () =
        (* if needed *)
        super#shutdown ()
    end in

  object (self)
    method name = "worker"
    method create_processor _ _ _ =
      object (self)
        inherit Netplex_kit.processor_base hooks
        method process ~when_done _ _ _ = when_done () (* shouldn't
ever be called *)
        method supported_ptypes = [ `Multi_processing ]
      end
  end

Then add factory to the list of factories in Netplex_main.startup, and
add a section in your Netplex config file:

  service {
    protocol { name = "worker" };
    name = "worker";
    processor { type = "worker"; };
    workload_manager { type = "constant"; threads = 1; };
  };

One detail is that in order to receive messages from the Netplex
controller (in particular the shutdown message) you need to
periodically yield control back to the event loop, by setting a timer
to call you back. (With Lwt-equeue you can just use Lwt_unix.yield).

Jake

------------------------------------------------------------------------------
Create and Deploy Rich Internet Apps outside the browser with Adobe(R)AIR(TM)
software. With Adobe AIR, Ajax developers can use existing skills and code to
build responsive, highly engaging applications that combine the power of local
resources and data with the reach of the web. Download the Adobe AIR SDK and
Ajax docs to start building applications today-http://p.sf.net/sfu/adobe-com