Added rudimentary threading docs based on suggestion from Morarenko Kirill.

This commit is contained in:
Ryan Williams
2009-08-16 14:25:56 -07:00
parent b2aa119d80
commit c6b010dad1
3 changed files with 22 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

View File

@@ -42,6 +42,7 @@ Contents
basic_usage
chat_server_example
threading
history
modules

21
doc/threading.rst Normal file
View File

@@ -0,0 +1,21 @@
Using Eventlet with Threads
=============================
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.
.. image:: /images/threading_illustration.png
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.
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 tpool module is provided to make these uses simpler.
The simplest thing to do with tpool is to ``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::
>>> import thread
>>> from eventlet import tpool
>>> def my_func(starting_ident):
... print "running in new thread:", starting_ident != thread.get_ident()
>>> tpool.execute(my_func, thread.get_ident())
running in new thread: True
My 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.