Skip to content

Commit

Permalink
fix typo confusing fibo and factorial
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristian Rother committed Sep 19, 2024
1 parent 15e7817 commit a85080d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 20 deletions.
38 changes: 26 additions & 12 deletions concurrency/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,6 @@ Many times, the devil is so much in the details that it is even a bad idea.

----

Alternatives
------------

- if you want to read/write data, use a database
- if you want to scrape web pages, use the ``scrapy`` framework.
- if you want to build a web server, use ``FastAPI`, ``Flask`` or ``Django``.
- if you want to do number crunching, use ``Spark``, ``Dask``, ``Pytorch`` or ``Tensorflow``

----

When could concurrency be a good idea?
--------------------------------------

Expand All @@ -49,7 +39,7 @@ Multithreading
This is the old way to implement parallel execution.
It has its flaws but you can grasp the basic idea:

.. literalinclude:: multithreading.py
.. literalinclude:: thread_factorial.py

----

Expand All @@ -59,5 +49,29 @@ Async Coroutines
The `async` interface has been added to Python more recently.
It fixes many problems of threads.

.. literalinclude:: coroutine_asyncio.py
.. literalinclude:: async_factorial.py

----

Subprocesses
------------

The `subprocess` module allows you to launch extra processes through the operating system.
Subprocesses are not restricted to Python programs.
This is the most flexible approach, but also has the highest overhead.

.. literalinclude:: subprocess_factorial.py

.. literalinclude:: factorial.py


----

Alternatives
------------

- if you want to read/write data, use a database
- if you want to scrape web pages, use the ``scrapy`` framework.
- if you want to build a web server, use ``FastAPI`, ``Flask`` or ``Django``.
- if you want to do number crunching, use ``Spark``, ``Dask``, ``Pytorch`` or ``Tensorflow``

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def multiply(a, b):


async def factorial(number):
"""delayed calculation of fibonacci number"""
"""delayed calculation of factorial numbers"""
result = reduce(multiply, range(1, number + 1), 1)
delay = random.randint(5, 20)
await asyncio.sleep(delay)
Expand Down
2 changes: 1 addition & 1 deletion concurrency/fibonacci.py → concurrency/factorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@

delay = random.randint(5, 15)
time.sleep(delay)
print(f"fibonacci of {sys.argv[1]} = {result} after {delay} sec")
print(f"factorial of {sys.argv[1]} = {result} after {delay} sec")
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
import subprocess

# launch a single external process
# r = subprocess.run(["python", "fibonacci.py", str(5)])
# r = subprocess.run(["python", "factorial.py", str(5)])
# try some other command than Python


procs = []
for i in range(10):
cmd = ["python", "fibonacci.py", str(i)]
cmd = ["python", "factorial.py", str(i)]
p = subprocess.Popen(cmd) # , stdout=subprocess.PIPE)
# add stdout argument to see results immediately
procs.append(p)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Fibonacci with threads
Factorial with threads
# adopted from
http://www.devshed.com/c/a/Python/Basic-Threading-in-Python/1/
"""
Expand All @@ -9,7 +9,7 @@
import random


class FibonacciThread(threading.Thread):
class FactorialThread(threading.Thread):
def __init__(self, number):
super().__init__()
self.number = number
Expand All @@ -19,7 +19,7 @@ def fibo(n):
if n == 0:
return 1
else:
return n * FibonacciThread.fibo(n - 1)
return n * FactorialThread.fibo(n - 1)

def run(self):
result = self.fibo(self.number)
Expand All @@ -28,4 +28,4 @@ def run(self):


for number in range(10):
FibonacciThread(number).start()
FactorialThread(number).start()

0 comments on commit a85080d

Please sign in to comment.