Coding Guidelines/Asynchronous Programming: Difference between revisions

From wiki.zmanda.com
Jump to navigation Jump to search
(Previous comments were not necessary)
(link to Amanda::MainLoop)
 
Line 4: Line 4:
Historically, Amanda used the [[Event API]] to handle asynchronous programming.  As a general platform for structuring applications, this API was inadequate.  Luckily, Glib provides a similar, but more advanced, service called GMainLoop.  The [[Event API]] was rewritten as a wrapper around GMainLoop, and all subsequent code is expected to use GMainLoop directly.
Historically, Amanda used the [[Event API]] to handle asynchronous programming.  As a general platform for structuring applications, this API was inadequate.  Luckily, Glib provides a similar, but more advanced, service called GMainLoop.  The [[Event API]] was rewritten as a wrapper around GMainLoop, and all subsequent code is expected to use GMainLoop directly.


= Usage =
= C Usage =
For general usage of GMainLoop, please see the glib documentation.  Remember that Amanda requires [[Installation/Amanda's Requirements|glib-2.2.0 or higher]], so any functionality used must be present in glib-2.2.0.
For general usage of GMainLoop, please see the glib documentation.  Remember that Amanda requires [[Installation/Amanda's Requirements|glib-2.2.0 or higher]], so any functionality used must be present in glib-2.2.0.


Line 11: Line 11:
or
or
  g_main_loop_quit(default_main_loop());
  g_main_loop_quit(default_main_loop());
= Perl Usage =
See {{pod|Amanda::MainLoop}} for an example and a list of available event sources.

Latest revision as of 14:04, 15 August 2008

Asynchronous programming is a way of writing code which reacts to events, rather than churning mercilessly through a fixed process. In essence, the program consists of a "main loop" which waits for events to occur, and a dispatcher which invokes the appropriate piece of code to handle each event. This style is usually used when writing network daemons, where the events are incoming data from clients and timeouts.

History

Historically, Amanda used the Event API to handle asynchronous programming. As a general platform for structuring applications, this API was inadequate. Luckily, Glib provides a similar, but more advanced, service called GMainLoop. The Event API was rewritten as a wrapper around GMainLoop, and all subsequent code is expected to use GMainLoop directly.

C Usage

For general usage of GMainLoop, please see the glib documentation. Remember that Amanda requires glib-2.2.0 or higher, so any functionality used must be present in glib-2.2.0.

The default GMainLoop object is available from event.h's default_main_loop(), so you might write

g_main_loop_run(default_main_loop());

or

g_main_loop_quit(default_main_loop());

Perl Usage

See Amanda::MainLoop for an example and a list of available event sources.