Basic Usage
***********

If it's your first time to Eventlet, you may find the illuminated
examples in the Design Patterns document to be a good starting point.

Eventlet is built around the concept of green threads (i.e.
coroutines, we use the terms interchangeably) that are launched to do
network-related work.  Green threads differ from normal threads in two
main ways:

* Green threads are so cheap they are nearly free.  You do not have
  to conserve green threads like you would normal threads.  In
  general, there will be at least one green thread per network
  connection.

* Green threads cooperatively yield to each other instead of
  preemptively being scheduled.  The major advantage from this
  behavior is that shared data structures don't need locks, because
  only if a yield is explicitly called can another green thread have
  access to the data structure.  It is also possible to inspect
  primitives such as queues to see if they have any pending data.


Primary API
***********

The design goal for Eventlet's API is simplicity and readability.  You
should be able to read its code and understand what it's doing.  Fewer
lines of code are preferred over excessively clever implementations.
Like Python itself, there should be one, and only one obvious way to
do it in Eventlet!

Though Eventlet has many modules, much of the most-used stuff is
accessible simply by doing "import eventlet".  Here's a quick summary
of the functionality available in the "eventlet" module, with links to
more verbose documentation on each.


Greenthread Spawn
=================

eventlet.spawn(func, *args, **kw)

   This launches a greenthread to call *func*.  Spawning off multiple
   greenthreads gets work done in parallel.  The return value from
   "spawn" is a "greenthread.GreenThread" object, which can be used to
   retrieve the return value of *func*.  See "spawn" for more details.

eventlet.spawn_n(func, *args, **kw)

   The same as "spawn()", but it's not possible to know how the
   function terminated (i.e. no return value or exceptions).  This
   makes execution faster.  See "spawn_n" for more details.

eventlet.spawn_after(seconds, func, *args, **kw)

   Spawns *func* after *seconds* have elapsed; a delayed version of
   "spawn()".   To abort the spawn and prevent *func* from being
   called, call "GreenThread.cancel()" on the return value of
   "spawn_after()".  See "spawn_after" for more details.


Greenthread Control
===================

eventlet.sleep(seconds=0)

   Suspends the current greenthread and allows others a chance to
   process.  See "sleep" for more details.

class eventlet.GreenPool

   Pools control concurrency.  It's very common in applications to
   want to consume only a finite amount of memory, or to restrict the
   amount of connections that one part of the code holds open so as to
   leave more for the rest, or to behave consistently in the face of
   unpredictable input data.  GreenPools provide this control.  See
   "GreenPool" for more on how to use these.

class eventlet.GreenPile

   GreenPile objects represent chunks of work.  In essence a GreenPile
   is an iterator that can be stuffed with work, and the results read
   out later. See "GreenPile" for more details.

class eventlet.Queue

   Queues are a fundamental construct for communicating data between
   execution units.  Eventlet's Queue class is used to communicate
   between greenthreads, and provides a bunch of useful features for
   doing that.  See "Queue" for more details.

class eventlet.Timeout

   This class is a way to add timeouts to anything.  It raises
   *exception* in the current greenthread after *timeout* seconds.
   When *exception* is omitted or "None", the Timeout instance itself
   is raised.

   Timeout objects are context managers, and so can be used in with
   statements. See "Timeout" for more details.


Patching Functions
==================

eventlet.import_patched(modulename, *additional_modules, **kw_additional_modules)

   Imports a module in a way that ensures that the module uses "green"
   versions of the standard library modules, so that everything works
   nonblockingly.  The only required argument is the name of the
   module to be imported.  For more information see Import Green.

eventlet.monkey_patch(all=True, os=False, select=False, socket=False, thread=False, time=False)

   Globally patches certain system modules to be greenthread-friendly.
   The keyword arguments afford some control over which modules are
   patched. If *all* is True, then all modules are patched regardless
   of the other arguments. If it's False, then the rest of the keyword
   arguments control patching of specific subsections of the standard
   library.  Most patch the single module of the same name (os, time,
   select).  The exceptions are socket, which also patches the ssl
   module if present; and thread, which patches thread, threading, and
   Queue.  It's safe to call monkey_patch multiple times.  For more
   information see Monkeypatching the Standard Library.


Network Convenience Functions
=============================

eventlet.connect(addr, family=<AddressFamily.AF_INET: 2>, bind=None)

   Convenience function for opening client sockets.

   Parameters:
      * **addr** -- Address of the server to connect to.  For TCP
        sockets, this is a (host, port) tuple.

      * **family** -- Socket family, optional.  See "socket"
        documentation for available families.

      * **bind** -- Local address to bind to, optional.

   Returns:
      The connected green socket object.

eventlet.listen(addr, family=<AddressFamily.AF_INET: 2>, backlog=50)

   Convenience function for opening server sockets.  This socket can
   be used in "serve()" or a custom "accept()" loop.

   Sets SO_REUSEADDR on the socket to save on annoyance.

   Parameters:
      * **addr** -- Address to listen on.  For TCP sockets, this is
        a (host, port)  tuple.

      * **family** -- Socket family, optional.  See "socket"
        documentation for available families.

      * **backlog** -- The maximum number of queued connections.
        Should be at least 1; the maximum value is system-dependent.

   Returns:
      The listening green socket object.

eventlet.wrap_ssl(sock, *a, **kw)

   Convenience function for converting a regular socket into an SSL
   socket.  Has the same interface as "ssl.wrap_socket()", but can
   also use PyOpenSSL. Though, note that it ignores the *cert_reqs*,
   *ssl_version*, *ca_certs*, *do_handshake_on_connect*, and
   *suppress_ragged_eofs* arguments when using PyOpenSSL.

   The preferred idiom is to call wrap_ssl directly on the creation
   method, e.g., "wrap_ssl(connect(addr))" or "wrap_ssl(listen(addr),
   server_side=True)". This way there is no "naked" socket sitting
   around to accidentally corrupt the SSL session.

   :return Green SSL object.

eventlet.serve(sock, handle, concurrency=1000)

   Runs a server on the supplied socket.  Calls the function *handle*
   in a separate greenthread for every incoming client connection.
   *handle* takes two arguments: the client socket object, and the
   client address:

      def myhandle(client_sock, client_addr):
          print("client connected", client_addr)

      eventlet.serve(eventlet.listen(('127.0.0.1', 9999)), myhandle)

   Returning from *handle* closes the client socket.

   "serve()" blocks the calling greenthread; it won't return until the
   server completes.  If you desire an immediate return, spawn a new
   greenthread for "serve()".

   Any uncaught exceptions raised in *handle* are raised as exceptions
   from "serve()", terminating the server, so be sure to be aware of
   the exceptions your application can raise.  The return value of
   *handle* is ignored.

   Raise a "StopServe" exception to gracefully terminate the server --
   that's the only way to get the server() function to return rather
   than raise.

   The value in *concurrency* controls the maximum number of
   greenthreads that will be open at any time handling requests.  When
   the server hits the concurrency limit, it stops accepting new
   connections until the existing ones complete.

class eventlet.StopServe

   Exception class used for quitting "serve()" gracefully.

These are the basic primitives of Eventlet; there are a lot more out
there in the other Eventlet modules; check out the Module Reference.
