diff --git a/RESULT.md b/RESULT.md new file mode 100644 index 0000000..92802c3 --- /dev/null +++ b/RESULT.md @@ -0,0 +1,74 @@ +io-bound +Синхронный код + +![img_1.png](imgs/io-bound/время-синхронный.jpg) + +![img_1.png](imgs/io-bound/диспетчер-синхронный.jpg) + +12 воркеров + +![img_1.png](imgs/io-bound/время12-воркеров.jpg) + +![img_1.png](imgs/io-bound/диспетчер12-воркеров.jpg) + +5 воркеров + +![img_2.png](imgs/io-bound/время5-воркеров.jpg) + +![img_2.png](imgs/io-bound/диспетчер5-воркеров.jpg) + +100 воркеров + +![img_2.png](imgs/io-bound/время100-воркеров.jpg) + +![img_2.png](imgs/io-bound/диспетчер100-воркеров.jpg) + +Итог: для io-bound задач увеличение количества потоков уменьшает время выполнение программы + +cpu-bound + +Синхронный код + +![img_1.png](imgs/cpu-bound/время-синхронно.jpg) + +![img_1.png](imgs/cpu-bound/диспетчер-синхронно.jpg) + +4 воркеров + +![img_1.png](imgs/cpu-bound/время4-воркера.jpg) + +![img_1.png](imgs/cpu-bound/диспетчер4-воркера.jpg) + +2 воркеров + +![img_2.png](imgs/cpu-bound/время2-воркера.jpg) + +![img_2.png](imgs/cpu-bound/диспетчер2-воркера.jpg) + +5 воркеров + +![img_2.png](imgs/cpu-bound/время5-воркера.jpg) + +![img_2.png](imgs/cpu-bound/диспетчер5-воркера.jpg) + +10 воркеров + +![img_2.png](imgs/cpu-bound/время10-воркера.jpg) + +![img_2.png](imgs/cpu-bound/диспетчер10-воркера.jpg) + +10 воркеров и 10 монет + +![img_2.png](imgs/cpu-bound/время-10монет10воркеров.jpg) + +![img_2.png](imgs/cpu-bound/диспетчер-10монет10воркеров.jpg) + +Вывод: ускорение работы получается только при увеличении количества создаваемых монет. В обратном случае наблюдается замедление + +При cpu-bound задачах увеличение потоков замедляет работу программы + +10 потоков и 10 монет + +![img_2.png](imgs/cpu-bound/время-io-10монет10воркеров.jpg) + +![img_2.png](imgs/cpu-bound/диспетчер-io-10монет10воркеров.jpg) \ No newline at end of file diff --git a/cpu-bound-synchronous.py b/cpu-bound-synchronous.py new file mode 100644 index 0000000..65d6c48 --- /dev/null +++ b/cpu-bound-synchronous.py @@ -0,0 +1,13 @@ +from hashlib import md5 +from random import choice + + +def makeHashes(): + j = 0 + while j != 5: + s = "".join([choice("0123456789") for _ in range(50)]) + h = md5(s.encode('utf8')).hexdigest() + + if h.endswith("00000"): + j += 1 + print(s, h) diff --git a/cpu-bound.py b/cpu-bound.py new file mode 100644 index 0000000..cad30ec --- /dev/null +++ b/cpu-bound.py @@ -0,0 +1,31 @@ +import concurrent.futures +from datetime import datetime +from hashlib import md5 +from random import choice + + +def make_hash(i): + while True: + s = "".join([choice("0123456789") for i in range(50)]) + h = md5(s.encode('utf8')).hexdigest() + if h.endswith("00000"): + return s, h + + +def makeMultiProcessHashes(): + with concurrent.futures.ProcessPoolExecutor(max_workers=10) as executor: + for s in executor.map(make_hash, range(10)): + print(s) + + +def makeMultiThreadHashes(): + with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor: + for s in executor.map(make_hash, range(10)): + print(s) + + +if __name__ == '__main__': + start_time = datetime.now() + makeMultiProcessHashes() + end_time = datetime.now() + print(end_time - start_time) diff --git "a/imgs/cpu-bound/\320\262\321\200\320\265\320\274\321\217-10\320\274\320\276\320\275\320\265\321\20210\320\262\320\276\321\200\320\272\320\265\321\200\320\276\320\262.jpg" "b/imgs/cpu-bound/\320\262\321\200\320\265\320\274\321\217-10\320\274\320\276\320\275\320\265\321\20210\320\262\320\276\321\200\320\272\320\265\321\200\320\276\320\262.jpg" new file mode 100644 index 0000000..796819a Binary files /dev/null and "b/imgs/cpu-bound/\320\262\321\200\320\265\320\274\321\217-10\320\274\320\276\320\275\320\265\321\20210\320\262\320\276\321\200\320\272\320\265\321\200\320\276\320\262.jpg" differ diff --git "a/imgs/cpu-bound/\320\262\321\200\320\265\320\274\321\217-io-10\320\274\320\276\320\275\320\265\321\20210\320\262\320\276\321\200\320\272\320\265\321\200\320\276\320\262.jpg" "b/imgs/cpu-bound/\320\262\321\200\320\265\320\274\321\217-io-10\320\274\320\276\320\275\320\265\321\20210\320\262\320\276\321\200\320\272\320\265\321\200\320\276\320\262.jpg" new file mode 100644 index 0000000..bc6f5c3 Binary files /dev/null and "b/imgs/cpu-bound/\320\262\321\200\320\265\320\274\321\217-io-10\320\274\320\276\320\275\320\265\321\20210\320\262\320\276\321\200\320\272\320\265\321\200\320\276\320\262.jpg" differ diff --git "a/imgs/cpu-bound/\320\262\321\200\320\265\320\274\321\217-\321\201\320\270\320\275\321\205\321\200\320\276\320\275\320\275\320\276.jpg" "b/imgs/cpu-bound/\320\262\321\200\320\265\320\274\321\217-\321\201\320\270\320\275\321\205\321\200\320\276\320\275\320\275\320\276.jpg" new file mode 100644 index 0000000..86af1b3 Binary files /dev/null and "b/imgs/cpu-bound/\320\262\321\200\320\265\320\274\321\217-\321\201\320\270\320\275\321\205\321\200\320\276\320\275\320\275\320\276.jpg" differ diff --git "a/imgs/cpu-bound/\320\262\321\200\320\265\320\274\321\21710-\320\262\320\276\321\200\320\272\320\265\321\200\320\260.jpg" "b/imgs/cpu-bound/\320\262\321\200\320\265\320\274\321\21710-\320\262\320\276\321\200\320\272\320\265\321\200\320\260.jpg" new file mode 100644 index 0000000..890e586 Binary files /dev/null and "b/imgs/cpu-bound/\320\262\321\200\320\265\320\274\321\21710-\320\262\320\276\321\200\320\272\320\265\321\200\320\260.jpg" differ diff --git "a/imgs/cpu-bound/\320\262\321\200\320\265\320\274\321\2172-\320\262\320\276\321\200\320\272\320\265\321\200\320\260.jpg" "b/imgs/cpu-bound/\320\262\321\200\320\265\320\274\321\2172-\320\262\320\276\321\200\320\272\320\265\321\200\320\260.jpg" new file mode 100644 index 0000000..48ee6a9 Binary files /dev/null and "b/imgs/cpu-bound/\320\262\321\200\320\265\320\274\321\2172-\320\262\320\276\321\200\320\272\320\265\321\200\320\260.jpg" differ diff --git "a/imgs/cpu-bound/\320\262\321\200\320\265\320\274\321\2174-\320\262\320\276\321\200\320\272\320\265\321\200\320\260.jpg" "b/imgs/cpu-bound/\320\262\321\200\320\265\320\274\321\2174-\320\262\320\276\321\200\320\272\320\265\321\200\320\260.jpg" new file mode 100644 index 0000000..07abc5b Binary files /dev/null and "b/imgs/cpu-bound/\320\262\321\200\320\265\320\274\321\2174-\320\262\320\276\321\200\320\272\320\265\321\200\320\260.jpg" differ diff --git "a/imgs/cpu-bound/\320\262\321\200\320\265\320\274\321\2175-\320\262\320\276\321\200\320\272\320\265\321\200\320\260.jpg" "b/imgs/cpu-bound/\320\262\321\200\320\265\320\274\321\2175-\320\262\320\276\321\200\320\272\320\265\321\200\320\260.jpg" new file mode 100644 index 0000000..338c2cb Binary files /dev/null and "b/imgs/cpu-bound/\320\262\321\200\320\265\320\274\321\2175-\320\262\320\276\321\200\320\272\320\265\321\200\320\260.jpg" differ diff --git "a/imgs/cpu-bound/\320\264\320\270\321\201\320\277\320\265\321\202\321\207\320\265\321\200-10\320\274\320\276\320\275\320\265\321\20210\320\262\320\276\321\200\320\272\320\265\321\200\320\276\320\262.jpg" "b/imgs/cpu-bound/\320\264\320\270\321\201\320\277\320\265\321\202\321\207\320\265\321\200-10\320\274\320\276\320\275\320\265\321\20210\320\262\320\276\321\200\320\272\320\265\321\200\320\276\320\262.jpg" new file mode 100644 index 0000000..faaf485 Binary files /dev/null and "b/imgs/cpu-bound/\320\264\320\270\321\201\320\277\320\265\321\202\321\207\320\265\321\200-10\320\274\320\276\320\275\320\265\321\20210\320\262\320\276\321\200\320\272\320\265\321\200\320\276\320\262.jpg" differ diff --git "a/imgs/cpu-bound/\320\264\320\270\321\201\320\277\320\265\321\202\321\207\320\265\321\200-io-10\320\274\320\276\320\275\320\265\321\20210\320\262\320\276\321\200\320\272\320\265\321\200\320\276\320\262.jpg" "b/imgs/cpu-bound/\320\264\320\270\321\201\320\277\320\265\321\202\321\207\320\265\321\200-io-10\320\274\320\276\320\275\320\265\321\20210\320\262\320\276\321\200\320\272\320\265\321\200\320\276\320\262.jpg" new file mode 100644 index 0000000..6a0b2de Binary files /dev/null and "b/imgs/cpu-bound/\320\264\320\270\321\201\320\277\320\265\321\202\321\207\320\265\321\200-io-10\320\274\320\276\320\275\320\265\321\20210\320\262\320\276\321\200\320\272\320\265\321\200\320\276\320\262.jpg" differ diff --git "a/imgs/cpu-bound/\320\264\320\270\321\201\320\277\320\265\321\202\321\207\320\265\321\200-\321\201\320\270\320\275\321\205\321\200\320\276\320\275\320\275\320\276.jpg" "b/imgs/cpu-bound/\320\264\320\270\321\201\320\277\320\265\321\202\321\207\320\265\321\200-\321\201\320\270\320\275\321\205\321\200\320\276\320\275\320\275\320\276.jpg" new file mode 100644 index 0000000..b2cba28 Binary files /dev/null and "b/imgs/cpu-bound/\320\264\320\270\321\201\320\277\320\265\321\202\321\207\320\265\321\200-\321\201\320\270\320\275\321\205\321\200\320\276\320\275\320\275\320\276.jpg" differ diff --git "a/imgs/cpu-bound/\320\264\320\270\321\201\320\277\320\265\321\202\321\207\320\265\321\20010-\320\262\320\276\321\200\320\272\320\265\321\200\320\260.jpg" "b/imgs/cpu-bound/\320\264\320\270\321\201\320\277\320\265\321\202\321\207\320\265\321\20010-\320\262\320\276\321\200\320\272\320\265\321\200\320\260.jpg" new file mode 100644 index 0000000..11834b3 Binary files /dev/null and "b/imgs/cpu-bound/\320\264\320\270\321\201\320\277\320\265\321\202\321\207\320\265\321\20010-\320\262\320\276\321\200\320\272\320\265\321\200\320\260.jpg" differ diff --git "a/imgs/cpu-bound/\320\264\320\270\321\201\320\277\320\265\321\202\321\207\320\265\321\2002-\320\262\320\276\321\200\320\272\320\265\321\200\320\260.jpg" "b/imgs/cpu-bound/\320\264\320\270\321\201\320\277\320\265\321\202\321\207\320\265\321\2002-\320\262\320\276\321\200\320\272\320\265\321\200\320\260.jpg" new file mode 100644 index 0000000..74a708c Binary files /dev/null and "b/imgs/cpu-bound/\320\264\320\270\321\201\320\277\320\265\321\202\321\207\320\265\321\2002-\320\262\320\276\321\200\320\272\320\265\321\200\320\260.jpg" differ diff --git "a/imgs/cpu-bound/\320\264\320\270\321\201\320\277\320\265\321\202\321\207\320\265\321\2004-\320\262\320\276\321\200\320\272\320\265\321\200\320\260.jpg" "b/imgs/cpu-bound/\320\264\320\270\321\201\320\277\320\265\321\202\321\207\320\265\321\2004-\320\262\320\276\321\200\320\272\320\265\321\200\320\260.jpg" new file mode 100644 index 0000000..bb7f24e Binary files /dev/null and "b/imgs/cpu-bound/\320\264\320\270\321\201\320\277\320\265\321\202\321\207\320\265\321\2004-\320\262\320\276\321\200\320\272\320\265\321\200\320\260.jpg" differ diff --git "a/imgs/cpu-bound/\320\264\320\270\321\201\320\277\320\265\321\202\321\207\320\265\321\2005-\320\262\320\276\321\200\320\272\320\265\321\200\320\260.jpg" "b/imgs/cpu-bound/\320\264\320\270\321\201\320\277\320\265\321\202\321\207\320\265\321\2005-\320\262\320\276\321\200\320\272\320\265\321\200\320\260.jpg" new file mode 100644 index 0000000..5c3a6ff Binary files /dev/null and "b/imgs/cpu-bound/\320\264\320\270\321\201\320\277\320\265\321\202\321\207\320\265\321\2005-\320\262\320\276\321\200\320\272\320\265\321\200\320\260.jpg" differ diff --git "a/imgs/io-bound/\320\262\321\200\320\265\320\274\321\217-\321\201\320\270\320\275\321\205\321\200\320\276\320\275\320\275\321\213\320\271.jpg" "b/imgs/io-bound/\320\262\321\200\320\265\320\274\321\217-\321\201\320\270\320\275\321\205\321\200\320\276\320\275\320\275\321\213\320\271.jpg" new file mode 100644 index 0000000..e2f591d Binary files /dev/null and "b/imgs/io-bound/\320\262\321\200\320\265\320\274\321\217-\321\201\320\270\320\275\321\205\321\200\320\276\320\275\320\275\321\213\320\271.jpg" differ diff --git "a/imgs/io-bound/\320\262\321\200\320\265\320\274\321\217100-\320\262\320\276\321\200\320\272\320\265\321\200\320\276\320\262.jpg" "b/imgs/io-bound/\320\262\321\200\320\265\320\274\321\217100-\320\262\320\276\321\200\320\272\320\265\321\200\320\276\320\262.jpg" new file mode 100644 index 0000000..ef15bb0 Binary files /dev/null and "b/imgs/io-bound/\320\262\321\200\320\265\320\274\321\217100-\320\262\320\276\321\200\320\272\320\265\321\200\320\276\320\262.jpg" differ diff --git "a/imgs/io-bound/\320\262\321\200\320\265\320\274\321\21712-\320\262\320\276\321\200\320\272\320\265\321\200\320\276\320\262.jpg" "b/imgs/io-bound/\320\262\321\200\320\265\320\274\321\21712-\320\262\320\276\321\200\320\272\320\265\321\200\320\276\320\262.jpg" new file mode 100644 index 0000000..f2531b3 Binary files /dev/null and "b/imgs/io-bound/\320\262\321\200\320\265\320\274\321\21712-\320\262\320\276\321\200\320\272\320\265\321\200\320\276\320\262.jpg" differ diff --git "a/imgs/io-bound/\320\262\321\200\320\265\320\274\321\2175-\320\262\320\276\321\200\320\272\320\265\321\200\320\276\320\262.jpg" "b/imgs/io-bound/\320\262\321\200\320\265\320\274\321\2175-\320\262\320\276\321\200\320\272\320\265\321\200\320\276\320\262.jpg" new file mode 100644 index 0000000..f9076c4 Binary files /dev/null and "b/imgs/io-bound/\320\262\321\200\320\265\320\274\321\2175-\320\262\320\276\321\200\320\272\320\265\321\200\320\276\320\262.jpg" differ diff --git "a/imgs/io-bound/\320\264\320\270\321\201\320\277\320\265\321\202\321\207\320\265\321\200-\321\201\320\270\320\275\321\205\321\200\320\276\320\275\320\275\321\213\320\271.jpg" "b/imgs/io-bound/\320\264\320\270\321\201\320\277\320\265\321\202\321\207\320\265\321\200-\321\201\320\270\320\275\321\205\321\200\320\276\320\275\320\275\321\213\320\271.jpg" new file mode 100644 index 0000000..0ae1ca0 Binary files /dev/null and "b/imgs/io-bound/\320\264\320\270\321\201\320\277\320\265\321\202\321\207\320\265\321\200-\321\201\320\270\320\275\321\205\321\200\320\276\320\275\320\275\321\213\320\271.jpg" differ diff --git "a/imgs/io-bound/\320\264\320\270\321\201\320\277\320\265\321\202\321\207\320\265\321\200100-\320\262\320\276\321\200\320\272\320\265\321\200\320\276\320\262.jpg" "b/imgs/io-bound/\320\264\320\270\321\201\320\277\320\265\321\202\321\207\320\265\321\200100-\320\262\320\276\321\200\320\272\320\265\321\200\320\276\320\262.jpg" new file mode 100644 index 0000000..0990d55 Binary files /dev/null and "b/imgs/io-bound/\320\264\320\270\321\201\320\277\320\265\321\202\321\207\320\265\321\200100-\320\262\320\276\321\200\320\272\320\265\321\200\320\276\320\262.jpg" differ diff --git "a/imgs/io-bound/\320\264\320\270\321\201\320\277\320\265\321\202\321\207\320\265\321\20012-\320\262\320\276\321\200\320\272\320\265\321\200\320\276\320\262.jpg" "b/imgs/io-bound/\320\264\320\270\321\201\320\277\320\265\321\202\321\207\320\265\321\20012-\320\262\320\276\321\200\320\272\320\265\321\200\320\276\320\262.jpg" new file mode 100644 index 0000000..0f32f92 Binary files /dev/null and "b/imgs/io-bound/\320\264\320\270\321\201\320\277\320\265\321\202\321\207\320\265\321\20012-\320\262\320\276\321\200\320\272\320\265\321\200\320\276\320\262.jpg" differ diff --git "a/imgs/io-bound/\320\264\320\270\321\201\320\277\320\265\321\202\321\207\320\265\321\2005-\320\262\320\276\321\200\320\272\320\265\321\200\320\276\320\262.jpg" "b/imgs/io-bound/\320\264\320\270\321\201\320\277\320\265\321\202\321\207\320\265\321\2005-\320\262\320\276\321\200\320\272\320\265\321\200\320\276\320\262.jpg" new file mode 100644 index 0000000..56abc2a Binary files /dev/null and "b/imgs/io-bound/\320\264\320\270\321\201\320\277\320\265\321\202\321\207\320\265\321\2005-\320\262\320\276\321\200\320\272\320\265\321\200\320\276\320\262.jpg" differ diff --git a/io-bound-synchronous.py b/io-bound-synchronous.py new file mode 100644 index 0000000..0c4a1a1 --- /dev/null +++ b/io-bound-synchronous.py @@ -0,0 +1,35 @@ +from urllib.request import Request, urlopen +from bs4 import BeautifulSoup +from tqdm import tqdm + +url = 'https://ru.wikipedia.org/wiki/%D0%A1%D0%BB%D1%83%D0%B6%D0%B5%D0%B1%D0%BD%D0%B0%D1%8F:%D0%A1%D0%BB%D1%83%D1%87%D0%B0%D0%B9%D0%BD%D0%B0%D1%8F_%D1%81%D1%82%D1%80%D0%B0%D0%BD%D0%B8%D1%86%D0%B0' + + +def writeRefs(): + with open('res.txt', 'w', encoding='utf8') as res: + for i in tqdm(range(100)): + html = urlopen(url).read().decode('utf8') + soup = BeautifulSoup(html, 'html.parser') + links = soup.find_all('a') + + for l in links: + href = l.get('href') + if href and href.startswith('http') and 'wiki' not in href: + print(href, file=res) + + +def readRefs(): + with open('res.txt', encoding='utf8') as links: + for url in links.readlines(): + try: + request = Request( + url, + headers={ + 'User-Agent': 'Mozilla/5.0 (Windows NT 9.0; Win65; x64; rv:97.0) Gecko/20105107 Firefox/92.0'}, + ) + resp = urlopen(request, timeout=5) + code = resp.code + print(code) + resp.close() + except Exception as e: + print(url, e) \ No newline at end of file diff --git a/io-bound_task.py b/io-bound_task.py new file mode 100644 index 0000000..124b14c --- /dev/null +++ b/io-bound_task.py @@ -0,0 +1,29 @@ +import concurrent.futures +import urllib +from datetime import datetime +from urllib.request import urlopen + + +def load_url(url, timeout): + with urllib.request.urlopen(url, timeout=timeout) as resp: + code = resp.code + return code + + +def readMultiThreadRefs(): + with open('res.txt', encoding='utf8') as links: + with concurrent.futures.ThreadPoolExecutor(max_workers=100) as executor: + future_urls = {executor.submit(load_url, url, 5): url for url in links.readlines()} + for future in concurrent.futures.as_completed(future_urls): + url = future_urls[future] + try: + data = future.result() + print(data) + except Exception as e: + print(url, e) + + +start_time = datetime.now() +readMultiThreadRefs() +end_time = datetime.now() +print(end_time - start_time)