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
74 changes: 74 additions & 0 deletions RESULT.md
Original file line number Diff line number Diff line change
@@ -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)
13 changes: 13 additions & 0 deletions cpu-bound-synchronous.py
Original file line number Diff line number Diff line change
@@ -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)
31 changes: 31 additions & 0 deletions cpu-bound.py
Original file line number Diff line number Diff line change
@@ -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)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 imgs/cpu-bound/время-синхронно.jpg
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 imgs/cpu-bound/время10-воркера.jpg
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 imgs/cpu-bound/время2-воркера.jpg
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 imgs/cpu-bound/время4-воркера.jpg
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 imgs/cpu-bound/время5-воркера.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 imgs/io-bound/время-синхронный.jpg
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 imgs/io-bound/время100-воркеров.jpg
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 imgs/io-bound/время12-воркеров.jpg
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 imgs/io-bound/время5-воркеров.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions io-bound-synchronous.py
Original file line number Diff line number Diff line change
@@ -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)
29 changes: 29 additions & 0 deletions io-bound_task.py
Original file line number Diff line number Diff line change
@@ -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)