Add python-eventlet 0.16.1
[packages/trusty/python-eventlet.git] / eventlet / doc / threading.rst
1 Threads
2 ========
3
4 Eventlet is thread-safe and can be used in conjunction with normal Python threads.  The way this works is that coroutines are confined to their 'parent' Python thread.  It's like each thread contains its own little world of coroutines that can switch between themselves but not between coroutines in other threads.
5
6 .. image:: /images/threading_illustration.png
7
8 You can only communicate cross-thread using the "real" thread primitives and pipes.  Fortunately, there's little reason to use threads for concurrency when you're already using coroutines.
9
10 The vast majority of the times you'll want to use threads are to wrap some operation that is not "green", such as a C library that uses its own OS calls to do socket operations.  The :mod:`~eventlet.tpool` module is provided to make these uses simpler.
11
12 The optional :ref:`pyevent hub <understanding_hubs>` is not compatible with threads.
13
14 Tpool - Simple thread pool
15 ---------------------------
16
17 The simplest thing to do with :mod:`~eventlet.tpool` is to :func:`~eventlet.tpool.execute` a function with it.  The function will be run in a random thread in the pool, while the calling coroutine blocks on its completion::
18
19  >>> import thread
20  >>> from eventlet import tpool
21  >>> def my_func(starting_ident):
22  ...     print("running in new thread:", starting_ident != thread.get_ident())
23  ...
24  >>> tpool.execute(my_func, thread.get_ident())
25  running in new thread: True
26
27 By default there are 20 threads in the pool, but you can configure this by setting the environment variable ``EVENTLET_THREADPOOL_SIZE`` to the desired pool size before importing tpool.
28
29 .. automodule:: eventlet.tpool
30         :members: