This repository has been archived by the owner on Jun 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
0716023_hw1.py
154 lines (119 loc) · 3.6 KB
/
0716023_hw1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import sys
from threading import Thread, Lock
import multiprocessing as mp
from multiprocessing import Process
from hashlib import sha256
from time import time as currentTimeInSeconds
import asyncio
from typing import List, Optional, Tuple
from random import randrange, shuffle
from math import ceil
import grequests
import requests
from lxml.html import fromstring
def currentTimeInMilli():
return int(currentTimeInSeconds() * 1000)
R_ASCII = 0x7e
L_ASCII = 0x21
LEN_ASCII = R_ASCII - L_ASCII + 1
def getPrefixCandidate(index: int):
ret = ''
for i in range(5):
ret += chr(index % LEN_ASCII + L_ASCII)
index = index // LEN_ASCII
return ret
def getRandomPrefixCandidate():
return getPrefixCandidate(randrange(LEN_ASCII ** 5))
def hash(s: str):
return sha256(s.encode()).hexdigest()
def findPrefix(list: List[str]):
for s in list:
while True:
p = getRandomPrefixCandidate()
tested = p + s
h = hash(tested)
if h[:5] == '00000':
print(tested)
break
async def findPrefixAsync(suffix: str):
while True:
p = getRandomPrefixCandidate()
tested = p + suffix
h = hash(tested)
if h[:5] == '00000':
print(tested)
break
def findTitle(urls: List[str]):
for url in urls:
r = requests.get(url)
tree = fromstring(r.content)
title = tree.findtext('.//title')
print(title)
def findTitleAsync(res, **kwargs):
tree = fromstring(res.content)
title = tree.findtext('.//title')
print(title)
def solveByThreads(m: int, data: List[str], threadCount: int):
n = len(data)
threadList = []
qn = ceil(n / threadCount)
tar = findPrefix if m == 1 else findTitle
for i in range(threadCount):
t = Thread(target=tar, args=(data[i * qn:i * qn + qn], ))
t.daemon = True
threadList.append(t)
t0 = currentTimeInMilli()
for t in threadList:
t.start()
for t in threadList:
t.join()
t1 = currentTimeInMilli()
return t1 - t0
def solveByProcesses(m: int, data: List[str], processCount: int):
n = len(data)
processList = []
qn = n // processCount
tar = findPrefix if m == 1 else findTitle
for i in range(processCount):
p = Process(target=tar, args=(data[i*qn:i*qn+qn], ))
p.daemon = True
processList.append(p)
t0 = currentTimeInMilli()
for p in processList:
p.start()
for p in processList:
p.join()
t1 = currentTimeInMilli()
return t1 - t0
async def solveByCoroutine(m: int, data: List[str]):
n = len(data)
tar = findPrefixAsync if m == 1 else findTitleAsync
t0 = currentTimeInMilli()
if m == 1:
await asyncio.gather(*(list(tar(v) for v in data)))
else:
async_list = []
for url in data:
action_item = grequests.get(
url, hooks={'response': findTitleAsync})
async_list.append(action_item)
grequests.map(async_list)
t1 = currentTimeInMilli()
return t1 - t0
def main():
workloadId = int(input())
measurement = list(int(x) for x in input().split())
n = int(input())
data = []
for _ in range(n):
data.append(input())
shuffle(data)
if measurement[0] == 1:
t = solveByThreads(workloadId, data, measurement[1])
elif measurement[0] == 2:
t = solveByProcesses(workloadId, data, measurement[1])
elif measurement[0] == 3:
t = asyncio.run(solveByCoroutine(workloadId, data))
print('Time: %d ms' % t)
if __name__ == '__main__':
main()