[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [stem/master] Add multiprocessing to tutorials



commit c458fcf9fafa727b9fb5b722c9f0dec2152c756a
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Tue Dec 12 11:48:43 2017 -0800

    Add multiprocessing to tutorials
    
    Ooh, this is a good candidate for our tutorials. Very handy module. Blogged
    these examples a few months back...
    
      http://blog.atagar.com/august2017/
---
 docs/_static/example/fibonacci_multiprocessing.py | 20 +++++++++++++
 docs/_static/example/fibonacci_threaded.py        | 24 ++++++++++++++++
 docs/tutorials/east_of_the_sun.rst                | 35 +++++++++++++++++++++++
 3 files changed, 79 insertions(+)

diff --git a/docs/_static/example/fibonacci_multiprocessing.py b/docs/_static/example/fibonacci_multiprocessing.py
new file mode 100644
index 00000000..e909e169
--- /dev/null
+++ b/docs/_static/example/fibonacci_multiprocessing.py
@@ -0,0 +1,20 @@
+import stem.util.system
+import time
+
+def fibonacci(n):
+  if n < 2:
+    return n
+  else:
+    return fibonacci(n-2) + fibonacci(n-1)
+
+# calculate fibonacci sequences four times in parallel
+
+start_time, threads = time.time(), []
+
+for i in range(4):
+  threads.append(stem.util.system.DaemonTask(fibonacci, (35,), start = True))
+
+for t in threads:
+  t.join()
+
+print('took %0.1f seconds' % (time.time() - start_time))
diff --git a/docs/_static/example/fibonacci_threaded.py b/docs/_static/example/fibonacci_threaded.py
new file mode 100644
index 00000000..f40b7401
--- /dev/null
+++ b/docs/_static/example/fibonacci_threaded.py
@@ -0,0 +1,24 @@
+import threading
+import time
+
+def fibonacci(n):
+  if n < 2:
+    return n
+  else:
+    return fibonacci(n-2) + fibonacci(n-1)
+
+# calculate fibonacci sequences four times in parallel
+
+start_time, threads = time.time(), []
+
+for i in range(4):
+  t = threading.Thread(target = fibonacci, args = (35,))
+  t.setDaemon(True)
+  t.start()
+
+  threads.append(t)
+
+for t in threads:
+  t.join()
+
+print('took %0.1f seconds' % (time.time() - start_time))
diff --git a/docs/tutorials/east_of_the_sun.rst b/docs/tutorials/east_of_the_sun.rst
index 845cbf4a..fe09c4d3 100644
--- a/docs/tutorials/east_of_the_sun.rst
+++ b/docs/tutorials/east_of_the_sun.rst
@@ -4,6 +4,7 @@ East of the Sun & West of the Moon
 The following is an overview of some of the utilities Stem provides.
 
 * :ref:`terminal-styling`
+* :ref:`multiprocessing`
 * :ref:`connection-resolution`
 
 .. _terminal-styling:
@@ -28,6 +29,39 @@ yourself is easy, but we also provide a module to make it `even easier
 .. literalinclude:: /_static/example/words_with.py
    :language: python
 
+.. _multiprocessing:
+
+Multiprocessing
+---------------
+
+Python's `multiprocessing module
+<https://docs.python.org/2/library/multiprocessing.html>`_ gives building
+blocks to parallelize around the `Global Interpreter Lock
+<https://en.wikipedia.org/wiki/Global_interpreter_lock>`_. However, honestly
+it's clunky to use.
+
+Ever just wanted to simply turn your threads into subprocesses? `We can do
+that <../api/util/system.html#stem.util.system.DaemonTask>`_.
+
+**Threaded**
+
+.. literalinclude:: /_static/example/fibonacci_threaded.py
+   :language: python
+
+::
+
+  % python fibonacci_threaded.py
+  took 21.1 seconds
+
+**Multi-process**
+
+.. literalinclude:: /_static/example/fibonacci_multiprocessing.py
+   :language: python
+
+::
+
+  % python fibonacci_multiprocessing.py
+  took 6.2 seconds
 .. _connection-resolution:
 
 Connection Resolution
@@ -69,3 +103,4 @@ simple script that dumps Tor's present connections.
 
     192.168.0.1:59014 => 38.229.79.2:443
     192.168.0.1:58822 => 68.169.35.102:443
+



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits