我现在有80个用户名的列表,我有我的脚本检查每个用户名是否存在。然而,它需要比我喜欢的时间长一点,所以我想知道是否有任何事情可以做,以加快需要多长时间来检查每个用户名是否存在。任何可能的方式来加快处理这个?
# ------------------------------
# Mass Kik Username Checker
# Script Made by: Ski
# ------------------------------
import requests, threading
def check(username):
try:
req = requests.get("http://kik.me/"+username, allow_redirects=False).status_code
if req == 302:
return False
if req == 200:
return True
except Exception as e:
print e
exit()
def _loadList(filename):
item_list = []
for item in str(open(filename, "r").read()).split("\n"):
item_list.append(item)
return item_list
def _thread(items):
global _usernames
for username in _usernames[items[0]:items[1]]:
exists = check(username)
if exists:
print username+" exists\n"
if not exists:
print username+" doesn't exist\n"
if __name__ == '__main__':
_usernames = _loadList("usernames.txt")
thread1 = threading.Thread(target=_thread, args=([0, 20],)).start()
thread2 = threading.Thread(target=_thread, args=([20, 40],)).start()
thread3 = threading.Thread(target=_thread, args=([40, 60],)).start()
thread4 = threading.Thread(target=_thread, args=([60, 80],)).start()
使用概要分析来确定你的瓶颈 – Moritz
莫里茨也是一个不错的建议,但如果它是不完全清楚,他是指的是'profile'和'cProfile'标准库模块。 'cProfile'是首选。 –
创建超过4个工作线程的池,为什么不是80或更多。并使用队列与线程通信,而不是全局变量(这不是为了性能,而是为了代码的正确性)。 – uselpa