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
23 changes: 23 additions & 0 deletions cpubound.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
import math

PRIMES = [0,0,0,0]

def is_prime(n):
while True:
s = "".join([choice("0123456789") for i in range(50)])
h = md5(s.encode('utf8')).hexdigest()
if h.endswith("00000"):
n = str(s) + ' ' + str(h)
break
return n

def main():
with concurrent.futures.ProcessPoolExecutor(max_workers=61) as executor:
for prime in zip(PRIMES, executor.map(is_prime, PRIMES)):
print(prime)

if __name__ == '__main__':
main()
17 changes: 17 additions & 0 deletions iobound.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from urllib.request import Request, urlopen
from urllib.parse import unquote

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

for url in links:
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)
22 changes: 22 additions & 0 deletions iobound2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from urllib.request import Request, urlopen
from urllib.parse import unquote
import concurrent.futures
import urllib.request

links = open('res.txt', encoding='utf8').read().splitlines()

# 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.code

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('%r generated an exception: %s' % (url, exc))
else:
print(f'{data:d}')
Loading