Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions REPORT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Параллелизм и асинхронность

## IO-bound

Синхронно в 1 поток:
![img](img/sync.png)

5 воркеров:
![img](img/five.png)
![img](img/mem_5.png)

10 воркеров:
![img](img/ten.png)
![img](img/mem_10.png)

100 воркеров:
![img](img/hundred.png)
![img](img/mem_100.png)


Количество задействованной памяти и нагрузка на ЦП почти
не изменяется, так как из-за большого количества воркеров
происходит быстрое взаимодействие с сетью.

## CPU-bound

Генерация на 1 ядре для 4 монет:
![img](img/gen1.png)

2 воркера для 4 монет:
![img](img/two.png)
![img](img/mem_2c.png)

4 воркера для 4 монет:
![img](img/four.png)
![img](img/mem_4c.png)

5 воркеров для 4 монет:
![img](img/fiveC.png)
![img](img/mem_5c.png)

10 воркеров для 4 монет:
![img](img/tenC.png)
![img](img/mem_10c.png)

100 воркеров для 4 монет:
Будет выдавать ошибку, так как есть ограничение в 61 воркер
![img](img/error.png)

Протестировав программу пару раз, я пришел к выводу,
что увеличение или уменьшение кол-ва воркеров никак
не влияет на конечное время работы программы. Загруженность ЦП
никак не мог нормально уловить, так как программа буквально
за считанные секунды выполняла работу. Такой разброс во времени
обусловлен рандомным нахождением нужных нам монет.
(Процессор 6 ядер 12 потоков)
23 changes: 23 additions & 0 deletions cpu-bound.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from hashlib import md5
from random import choice
import concurrent.futures


def coin_generator(zero):
while True:
s = "".join([choice("0123456789") for i in range(50)])
h = md5(s.encode('utf8')).hexdigest()

if h.endswith("0000"):
return f"{s} {h}"


def main():
with concurrent.futures.ProcessPoolExecutor(max_workers=61) as executor:
for coin in zip(executor.map(coin_generator, "0000")):
print(coin)


if __name__ == '__main__':
main()

18 changes: 18 additions & 0 deletions get_links.py
Original file line number Diff line number Diff line change
@@ -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)
Binary file added img/error.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/five.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/fiveC.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/four.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/gen1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/hundred.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/mem_10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/mem_100.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/mem_10c.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/mem_2c.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/mem_4c.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/mem_5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/mem_5c.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/sync.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/ten.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/tenC.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/two.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions io-bound.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import urllib.request
import concurrent.futures

links = open('res.txt', encoding='utf8').read().split('\n')


def load_url(url, timeout):
with urllib.request.urlopen(url, timeout=timeout) as conn:
return conn.read()


with concurrent.futures.ThreadPoolExecutor(max_workers=100) as executor:
future_to_url = {executor.submit(load_url, url, 5): 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(url, exc)
else:
print(200)
1,188 changes: 1,188 additions & 0 deletions res.txt

Large diffs are not rendered by default.