On Tue, Oct 08, 2013 at 11:13:22PM -0400, Vikas N Kumar <[email protected]> 
wrote:
> I have a few  questions which if answered can help me further:
> - Does the tcp_server() call use the same event loop as the default
> AnyEvent loop when I do AnyEvent->condvar ?

tcp_server uses AnyEvent internally for any event handling, so it will be
compatible to any other code using AnyEvent (likewise any other module
that comes with AnyEvent, and usually any module with AnyEvent in its
name). In fact, the whole point of AnyEvent is that it enables people to
write such event-loop agnostic functions.

> If not, how does one access that event loop and does it run in a
> separate thread then ?

If with threads, you mean something like pthreads, then perl itself has no
support for that. Perl does have some windows process emulation which can
be enabled on unix as well, but AnyEvent doesn't support that (it might
work, if the event loop supports it, but no guarantees), and it's unlikely
to help much.

If you want to offload some cpu-intensive work, you *could* look into
AnyEvent::Fork (and AnyEvent::Fork::RPC and AnyEvent::Fork::Pool), which
start separate perl processes and let you communicate with them (quite
similar to the process emulation in perl, but likely with less overhead and
higher compatibility).

Depending in your task, you could also offload while tcp connections via
these modules, or you could run tcp_server in multiple subprocesses.

> - Can I use Coro to create 2 separate AnyEvent main loops ?

No, AnyEvent supports one main loop only. You wouldn't gain anything from
doing that anyway, one main loop is almost always more efficient with Coro
(Coro threads never run in parallel with each other).

Maybe some diagrams help to understand where (p-)threads and Coro/AnyEvent
would fit in:


Traditional p-threads are good for single-core, and scale reasonably well
to a low number of additional cores:

   <single cpu><multicore cpu><multiple nodes>
   <------threads----->

processes scale well to multiple cores, and reasonably well to multiple
nodes:

   <single cpu><multicore cpu><multiple nodes>
         <---------processes----------->

coro and anyevent do not scale to more than one core at all:

   <single cpu><multicore cpu><multiple nodes>
   <-ae+coro-->

while (for example) AnyEvent::Fork uses processes to scale well to
multiple cores:

   <single cpu><multicore cpu><multiple nodes>
             <---ae::fork---->

and things like AnyEvent::Fork::Remote or AnyEvent::MP scale to multiple
nodes:

   <single cpu><multicore cpu><multiple nodes>
             <---ae::fork::remote--->
             <----------AnyEvent::MP--------->

So, when in C++ you might want to got for threads for whatever reason
(usually availability of threading libraries is higher than libs for other
forms of parallel processing, such as MPI), you wouldn't want to do that
in Perl (lack of thread support), but instead would use AnyEvent within a
single process, and spawn multiple processes.

Within a single process, e.g. between Coro threads, communication is much
more efficient than with typical C++-threads, likewise thread switches are
way faster in Coro than e.g. with linuxthreads. Between multiple process,
communication is far less efficient, as you have to serialise all data,
but you can take better advantage of the existing hardware, and it often
isn't hard to then scale to multiple boxes.

The modules mentioned are only examples - there are many ways to
facilitate parallel processing - the modules above just happen to be the
ones I use (and wrote :).

> - How does one measure performance of an application using multiple
> events in AnyEvent ?

Using your favourite stopwatch, or any other means. Your question is way too
unspecific, I am afraid :)

-- 
                The choice of a       Deliantra, the free code+content MORPG
      -----==-     _GNU_              http://www.deliantra.net
      ----==-- _       generation
      ---==---(_)__  __ ____  __      Marc Lehmann
      --==---/ / _ \/ // /\ \/ /      [email protected]
      -=====/_/_//_/\_,_/ /_/\_\

_______________________________________________
anyevent mailing list
[email protected]
http://lists.schmorp.de/mailman/listinfo/anyevent

Reply via email to