diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..36eea99 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +IO-bound.py \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..d1e22ec --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..a0d3813 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/multi-task-at-19.iml b/.idea/multi-task-at-19.iml new file mode 100644 index 0000000..d0876a7 --- /dev/null +++ b/.idea/multi-task-at-19.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/CPU-bound.py b/CPU-bound.py new file mode 100644 index 0000000..b1a6305 --- /dev/null +++ b/CPU-bound.py @@ -0,0 +1,19 @@ +from hashlib import md5 +from random import choice +import concurrent.futures + +def get_coin(): + while True: + s = "".join([choice("0123456789") for i in range(50)]) + h = md5(s.encode('utf8')).hexdigest() + res = "" + if h.endswith("00000"): + return s, h + +def find_coin(workers, count): + with concurrent.futures.ProcessPoolExecutor(max_workers=workers) as executor: + for i in range(count): + executor.submit(get_coin) + +if __name__ == '__main__': + find_coin(61, 4) \ No newline at end of file diff --git a/Generate_link.py b/Generate_link.py new file mode 100644 index 0000000..c3000cf --- /dev/null +++ b/Generate_link.py @@ -0,0 +1,18 @@ +from urllib.request import urlopen +from urllib.parse import unquote +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' + +res = open('res.txt', 'w', encoding='utf8') + +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) diff --git a/IO-bound.py b/IO-bound.py new file mode 100644 index 0000000..e6bad0b --- /dev/null +++ b/IO-bound.py @@ -0,0 +1,22 @@ +import concurrent.futures +import urllib.request + +links = open('res.txt', encoding='utf8').read().split('\n') + +# Retrieve a single page and report the URL and contents +def load_url(url, timeout): + with urllib.request.urlopen(url, timeout=timeout) as conn: + return conn.read() + +# We can use a with statement to ensure threads are cleaned up promptly +with concurrent.futures.ThreadPoolExecutor(max_workers=100) as executor: + # Start the load operations and mark each future with its URL + future_to_url = {executor.submit(load_url, url, 60): url for url in links} + for future in concurrent.futures.as_completed(future_to_url): + url = future_to_url[future] + try: + data = future.result() + except Exception as exc: + print('%r generated an exception: %s' % (url, exc)) + else: + print('%r page is %d bytes' % (url, len(data))) \ No newline at end of file diff --git a/Pic/a1.png b/Pic/a1.png new file mode 100644 index 0000000..7321d05 Binary files /dev/null and b/Pic/a1.png differ diff --git a/Pic/a2.png b/Pic/a2.png new file mode 100644 index 0000000..88fe3f2 Binary files /dev/null and b/Pic/a2.png differ diff --git a/Pic/a3.png b/Pic/a3.png new file mode 100644 index 0000000..d50634c Binary files /dev/null and b/Pic/a3.png differ diff --git a/Pic/a4.png b/Pic/a4.png new file mode 100644 index 0000000..1a4b6b6 Binary files /dev/null and b/Pic/a4.png differ diff --git a/Pic/c1.png b/Pic/c1.png new file mode 100644 index 0000000..1a99b38 Binary files /dev/null and b/Pic/c1.png differ diff --git a/Pic/c2.png b/Pic/c2.png new file mode 100644 index 0000000..da42c04 Binary files /dev/null and b/Pic/c2.png differ diff --git a/Pic/c3.png b/Pic/c3.png new file mode 100644 index 0000000..29da77f Binary files /dev/null and b/Pic/c3.png differ diff --git a/Pic/c4.png b/Pic/c4.png new file mode 100644 index 0000000..a63674c Binary files /dev/null and b/Pic/c4.png differ diff --git a/Pic/c5.png b/Pic/c5.png new file mode 100644 index 0000000..a2a53bf Binary files /dev/null and b/Pic/c5.png differ diff --git a/Pic/c6.png b/Pic/c6.png new file mode 100644 index 0000000..f869601 Binary files /dev/null and b/Pic/c6.png differ diff --git a/Result.md b/Result.md new file mode 100644 index 0000000..71c2877 --- /dev/null +++ b/Result.md @@ -0,0 +1,32 @@ +# Отчет по работе «Параллелизм и асинхронность» +IO-BOUND +Время работы IO_bound.py при синхронной проверке ссылок – 10057 ms + +![alt text](Pic/a1.png) + +Перепишем код, используя ThreadPoolExecutor. Благодар этому время работы изменилось. +Когда количество воркеров(потоков): +5 - 5460 ms +![alt text](Pic/a2.png) +10 - 4736 ms +![alt text](Pic/a3.png) +100 - 4176 ms +![alt text](Pic/a4.png) + +Таким образом, при увеличение количества потоков, увеличение используемой памяти и загрузки процессора не происходит, и при этом дает выигрыш в скорости (времени) выполнения кода. + +#CPU-BOUND +Время генерации четырех монет - 162764 ms +![alt text](Pic/c1.png) +Перепишем код используя ProcessPoolExecutor. +При max_workers=2 время работы - 125920 ms +![alt text](Pic/c2.png) +При max_workers=4 время работы - 71804 ms +![alt text](Pic/c3.png) +При max_workers=5 время работы - 61158 ms +![alt text](Pic/c4.png) +При max_workers=10 время работы - 57459 ms +![alt text](Pic/c5.png) +При max_workers=100 время работы - 71784 ms +![alt text](Pic/c6.png) +Увеличение количества процессов влечет за собой увеличение используемой памяти и загрузки процессора, но при этом дает выигрыш в скорости (времени) выполнения кода, если количество процессов не превышает числа физических ядер diff --git a/res.txt b/res.txt new file mode 100644 index 0000000..9e89d3b --- /dev/null +++ b/res.txt @@ -0,0 +1,17 @@ +http://www.warheroes.ru/hero/hero.asp?Hero_id=14666 +http://2.bp.blogspot.com/_bgY8-dFngM4/TQ82RVqrTKI/AAAAAAAAAJk/nBUHeDwei2I/s1600/O+zi+in+Mongolia+-+B.+Sharav+%281869-1939%29.jpg +http://www.zurag.de/zurag-film.html +https://libris.kb.se/katalogisering/hftx31m15ch5sm3 +http://pagad-ultimo.livejournal.com/5040.html +http://www.powys-society.org +https://www.treccani.it/enciclopedia/giovanni-maria-angiolello_(Dizionario-Biografico) +http://talishvestnik.narod.ru +http://talishvestnik.narod.ru/index.html +https://findsmi.ru/56946 +http://spider.seds.org/ngc/ngc.cgi?6636-1 +http://spider.seds.org/ngc/ngc_fr.cgi?6636-1 +http://spider.seds.org/ngc/revngcic.cgi?NGC6636-1 +http://vizier.u-strasbg.fr/viz-bin/VizieR-S?NGC+6636-1 +http://nedwww.ipac.caltech.edu/cgi-bin/nph-objsearch?objname=NGC+6636-1 +http://adsabs.harvard.edu/cgi-bin/basic_connect?qsearch=%22NGC+6636-1%22&version=1 +http://www.cbat.eps.harvard.edu/lists/Supernovae.html