Python: 如何同时多次使这个函数 运行
Python: How can I make this function run multiple times at the same time
所以基本上我有这个功能,我 运行 检查网站是否存在。问题是它需要太多时间。我有 50 个不同的链接要一一检查。有更快的方法吗?我目前正在使用 python 3。我听说过线程模块,但即使阅读了它,我仍然不确定如何使用它。你们有什么好的资源我可以阅读或查看以进一步了解它吗?
这是我的代码:
import requests
def checkExistingWeb():
for i in range(1, 51):
checkIfWebExist = requests.get("https://www.WEBSITE.com/ABCDE?id=" + str(i), allow_redirects=False)
if checkIfWebExist.status_code == 200:
print("3[92mWeb Exists!3[0m " + str(i))
else:
print("3[91mWeb Does Not Exist!3[0m " + str(i))
if __name__ == "__main__":
checkExistingWeb()
谢谢!
为一次执行创建函数,并在函数外部设置循环
executor = ThreadPoolExecutor()
checkweb_futures = [ executor.submit(checkExistWeb) for I in range(1, 51) ]
您可以使用multiprocessing.Pool
https://docs.python.org/2/library/multiprocessing.html
import requests, multiprocessing
def Check_Link(link):
checkIfWebExist = requests.get(link, allow_redirects=False)
if checkIfWebExist.status_code == 200:
print("3[92mWeb Exists!3[0m " + str(i))
else:
print("3[91mWeb Does Not Exist!3[0m " + str(i))
if __name__ == "__main__":
p = multiprocessing.Pool(5)
p.map(f, ["https://www.WEBSITE.com/ABCDE?id=" + str(i) for i in range(51)])
这段代码将创建一个线程池,并且 运行 这在 Check_Link 函数上
我真的很喜欢@Ronen 的回答,所以我研究了那个优秀的 ThreadPoolExecutor
class。他的回答不完整,因为您需要做更多的事情,因为在他的代码片段末尾的过程还没有完成。你需要处理那些 Futures
。这是一个扩展的答案:
executor = ThreadPoolExecutor()
checkweb_futures = [ executor.submit(checkExistWeb,I) for I in range(1, 51) ] # passed parameter
while not all(map(lambda x:x.done(),checkweb_futures)):
time.sleep(1)
checkweb_results = list(map(lambda x:x.results(),checkweb_futures))
现在您可以查看所有调用的结果。
是的,它对多线程很有用
import requests
def checkExistingWeb():
for i in range(1, 51):
checkIfWebExist = requests.get("https://www.WEBSITE.com/ABCDE?id=" + str(i), allow_redirects=False)
if checkIfWebExist.status_code == 200:
print("3[92mWeb Exists!3[0m " + str(i))
else:
print("3[91mWeb Does Not Exist!3[0m " + str(i))
if __name__ == "__main__":
with concurrent.futures.ThreadPoolExecutor() as executor:
results = executor.map(checkExistingWeb)
所以基本上我有这个功能,我 运行 检查网站是否存在。问题是它需要太多时间。我有 50 个不同的链接要一一检查。有更快的方法吗?我目前正在使用 python 3。我听说过线程模块,但即使阅读了它,我仍然不确定如何使用它。你们有什么好的资源我可以阅读或查看以进一步了解它吗?
这是我的代码:
import requests
def checkExistingWeb():
for i in range(1, 51):
checkIfWebExist = requests.get("https://www.WEBSITE.com/ABCDE?id=" + str(i), allow_redirects=False)
if checkIfWebExist.status_code == 200:
print("3[92mWeb Exists!3[0m " + str(i))
else:
print("3[91mWeb Does Not Exist!3[0m " + str(i))
if __name__ == "__main__":
checkExistingWeb()
谢谢!
为一次执行创建函数,并在函数外部设置循环
executor = ThreadPoolExecutor()
checkweb_futures = [ executor.submit(checkExistWeb) for I in range(1, 51) ]
您可以使用multiprocessing.Pool
https://docs.python.org/2/library/multiprocessing.html
import requests, multiprocessing
def Check_Link(link):
checkIfWebExist = requests.get(link, allow_redirects=False)
if checkIfWebExist.status_code == 200:
print("3[92mWeb Exists!3[0m " + str(i))
else:
print("3[91mWeb Does Not Exist!3[0m " + str(i))
if __name__ == "__main__":
p = multiprocessing.Pool(5)
p.map(f, ["https://www.WEBSITE.com/ABCDE?id=" + str(i) for i in range(51)])
这段代码将创建一个线程池,并且 运行 这在 Check_Link 函数上
我真的很喜欢@Ronen 的回答,所以我研究了那个优秀的 ThreadPoolExecutor
class。他的回答不完整,因为您需要做更多的事情,因为在他的代码片段末尾的过程还没有完成。你需要处理那些 Futures
。这是一个扩展的答案:
executor = ThreadPoolExecutor()
checkweb_futures = [ executor.submit(checkExistWeb,I) for I in range(1, 51) ] # passed parameter
while not all(map(lambda x:x.done(),checkweb_futures)):
time.sleep(1)
checkweb_results = list(map(lambda x:x.results(),checkweb_futures))
现在您可以查看所有调用的结果。
是的,它对多线程很有用
import requests
def checkExistingWeb():
for i in range(1, 51):
checkIfWebExist = requests.get("https://www.WEBSITE.com/ABCDE?id=" + str(i), allow_redirects=False)
if checkIfWebExist.status_code == 200:
print("3[92mWeb Exists!3[0m " + str(i))
else:
print("3[91mWeb Does Not Exist!3[0m " + str(i))
if __name__ == "__main__":
with concurrent.futures.ThreadPoolExecutor() as executor:
results = executor.map(checkExistingWeb)