├── .gitignore ├── README.md └── multiprocess ├── __init__.py ├── multiprocess.py └── tests.py /.gitignore: -------------------------------------------------------------------------------- 1 | __pycache__ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Multiprocessing In Python 2 | 3 | Calculating Fibonachi sequence siries and parallel; time comparizon! 4 | 5 | ## Roadmap 6 | 7 |
    8 |
  1. Roadmap
  2. 9 |
  3. 10 | Installation 11 |
  4. 12 |
  5. 13 | Usage/Examples 14 |
  6. 15 |
  7. Running Tests
  8. 16 |
  9. Acknowledgements
  10. 17 | 18 |
19 | 20 | --- 21 | 22 | ## Installation 23 | 24 | requires python >=3.6 25 | 26 | ```bash 27 | git clone https://github.com/MerlinEmris/multiprocessing_in_python.git 28 | ``` 29 | 30 | ```bash 31 | python multiproces\multiprocess.py 32 | ``` 33 | 34 | --- 35 | 36 | ## Usage/Examples 37 | 38 | App gets 2 variables from num to num2 39 | Application calculates Fibonachi secuence siries as normal python app and in parallel where task is seperated between cpu cores. 40 | 41 | ```bash 42 | python multiproces\multiprocess.py 12 22 43 | ``` 44 | 45 | --- 46 | 47 | ```bash 48 | python multiproces\multiprocess.py 1 40 49 | ``` 50 | 51 | ```bash 52 | cpu core count:16 53 | values are 1 - 40 54 | parallel: [[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 55 | 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155]] 56 | parallel time:15.866930961608887 57 | series: [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155] 58 | series time:36.89705181121826 59 | delta time (series/parallel):2.325405707032644 60 | ``` 61 | 62 | --- 63 | 64 | ## Running Tests 65 | 66 | To run tests, run the following command 67 | 68 | ```bash 69 | python multiproces\test.py 70 | ``` 71 | 72 | ## Acknowledgements 73 | 74 | - [Process-based parallelism](https://docs.python.org/3/library/multiprocessing.html) 75 | - [Multiprocessing in Python](https://www.geeksforgeeks.org/multiprocessing-python-set-1/) 76 | -------------------------------------------------------------------------------- /multiprocess/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MerlinEmris/multiprocessing_in_python/9d5804ce0eb10ab93a71530ad5ac7419003e6759/multiprocess/__init__.py -------------------------------------------------------------------------------- /multiprocess/multiprocess.py: -------------------------------------------------------------------------------- 1 | import multiprocessing as mp 2 | import json 3 | import time 4 | import sys 5 | 6 | 7 | def fib(n: int) -> int: 8 | """ 9 | Calculate n'th number in Fibonachi secuence 10 | @return n'th nubmer 11 | """ 12 | if n < 2: 13 | return 1 14 | else: 15 | return fib(n - 1) + fib(n - 2) 16 | 17 | 18 | def parallel(san1: int, san2: int) -> list: 19 | """ 20 | Calculates Fibonachi secuense between san1 and san2 parallel 21 | @return list of time taken by calculation, results 22 | """ 23 | pool = mp.Pool(processes=mp.cpu_count()) 24 | a: float = time.time() 25 | results = [pool.map(fib, (x for x in range(san1-1, san2)))] 26 | b: float = time.time() 27 | print("parallel: "+str(results)) 28 | print(f'parallel time:{b-a}') 29 | return [b-a, results] 30 | 31 | 32 | def normal(san1: int, san2: int) -> list: 33 | """ 34 | Calculates Fibonachi secuense between san1 and san2 series 35 | @return time taken by calculation 36 | """ 37 | results = [] 38 | a: float = time.time() 39 | for x in range(san1-1, san2): 40 | results.append(fib(x)) 41 | b: float = time.time() 42 | print("series: "+str(results)) 43 | print(f'series time:{b-a}') 44 | return [b-a, results] 45 | 46 | 47 | def resulter(san1: int, san2: int) -> tuple: 48 | """ 49 | Makes calculations siries and parallel 50 | @return san1, san2, series calc time, parallel calc time, series/parallel 51 | """ 52 | b: float = parallel(san1, san2)[0] 53 | a: float = normal(san1, san2)[0] 54 | print(f'delta time (series/parallel):{a/b}') 55 | return (san1, san2, a, b, a/b) 56 | 57 | 58 | def main(): 59 | data_start: int = 0 60 | data_end: int = 0 61 | print(f"cpu core count:{mp.cpu_count()}") 62 | try: 63 | data_start = int(sys.argv[1]) 64 | data_end = int(sys.argv[2]) 65 | print(f"values are {data_start} - {data_end}") 66 | except: 67 | data_start = 10 68 | data_end = 16 69 | print("values are 10 - 16") 70 | return resulter(data_start, data_end) 71 | 72 | 73 | if __name__ == '__main__': 74 | main() 75 | -------------------------------------------------------------------------------- /multiprocess/tests.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | import multiprocess as project 3 | 4 | 5 | class TestProject(unittest.TestCase): 6 | def test_fib(self): 7 | self.assertEqual(project.fib(3), 3) 8 | self.assertEqual(project.fib(4), 5) 9 | self.assertEqual(project.fib(5), 8) 10 | 11 | def test_parallel(self): 12 | self.assertEqual(project.parallel(1, 5)[1][0], [1, 1, 2, 3, 5]) 13 | 14 | def test_normal(self): 15 | self.assertEqual(project.normal(1, 5)[1], [1, 1, 2, 3, 5]) 16 | 17 | 18 | if __name__ == '__main__': 19 | unittest.main() 20 | --------------------------------------------------------------------------------