summaryrefslogtreecommitdiff
path: root/docs/python_threads.rst
diff options
context:
space:
mode:
authorJason Madden <jamadden@gmail.com>2020-11-19 10:59:41 -0600
committerJason Madden <jamadden@gmail.com>2020-11-19 10:59:41 -0600
commitdd2517b4ee50c2d08b98203e4c00682be5ae891f (patch)
tree4097048367da0cbaa906be00d02680448e416b9f /docs/python_threads.rst
parentac501f92a9ef5f3ec80b938647bb3f4919b4e924 (diff)
downloadgreenlet-dd2517b4ee50c2d08b98203e4c00682be5ae891f.tar.gz
More restructuring of the docs.docs
Diffstat (limited to 'docs/python_threads.rst')
-rw-r--r--docs/python_threads.rst44
1 files changed, 44 insertions, 0 deletions
diff --git a/docs/python_threads.rst b/docs/python_threads.rst
new file mode 100644
index 0000000..7389d3d
--- /dev/null
+++ b/docs/python_threads.rst
@@ -0,0 +1,44 @@
+==============================
+ Greenlets and Python Threads
+==============================
+
+Greenlets can be combined with Python threads; in this case, each thread
+contains an independent "main" greenlet with a tree of sub-greenlets. It
+is not possible to mix or switch between greenlets belonging to different
+threads.
+
+.. doctest::
+
+ >>> from greenlet import getcurrent
+ >>> from threading import Thread
+ >>> from threading import Event
+ >>> started = Event()
+ >>> switched = Event()
+ >>> class T(Thread):
+ ... def run(self):
+ ... self.glet = getcurrent()
+ ... started.set()
+ ... switched.wait()
+ >>> t = T()
+ >>> t.start()
+ >>> _ = started.wait()
+ >>> t.glet.switch()
+ Traceback (most recent call last):
+ ...
+ greenlet.error: cannot switch to a different thread
+ >>> switched.set()
+ >>> t.join()
+
+Note that when a thread dies, the thread's main greenlet is not
+considered to be dead.
+
+.. doctest::
+
+ >>> t.glet.dead
+ False
+
+.. caution::
+
+ For these reasons, it's best to not pass references to a greenlet
+ running in one thread to another thread. If you do, take caution to
+ carefully manage the lifetime of the references.