มีวิธีใดที่เป็นไปได้ในการเร่งการประมวลผลนี้ให้เร็วขึ้น?

ขณะนี้ฉันมีรายชื่อชื่อผู้ใช้ 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()

person vKi    schedule 18.10.2015    source แหล่งที่มา
comment
ใช้การทำโปรไฟล์เพื่อระบุจุดคอขวดของคุณ   -  person Moritz    schedule 18.10.2015
comment
Moritz ให้คำแนะนำที่ดี แต่ในกรณีที่ยังไม่ชัดเจนทั้งหมด เขาหมายถึงโมดูลไลบรารีมาตรฐาน profile และ cProfile cProfile เป็นที่ต้องการ   -  person X-Mann    schedule 18.10.2015
comment
สร้างพูลที่มีเธรดผู้ปฏิบัติงานมากกว่า 4 เธรด ทำไมไม่สร้าง 80 เธรดขึ้นไป และใช้คิวเพื่อสื่อสารกับเธรด ไม่ใช่ตัวแปรโกลบอล (นั่นไม่ใช่เพื่อประสิทธิภาพ แต่เพื่อความถูกต้องของโค้ด)   -  person uselpa    schedule 18.10.2015
comment
ใช้ เซสชัน สิ่งนี้จะทำให้คุณมีการเชื่อมต่อ HTTP แบบถาวร และทำให้คำขอไปยังโฮสต์เดียวกันเร็วขึ้นอย่างมาก นี่ควรเป็นสิ่งแรกที่คุณทำ ก่อนที่จะคิดถึงเรื่องมัลติเธรด   -  person Lukas Graf    schedule 18.10.2015


คำตอบ (1)


ลองใช้ Python 3.x กลุ่มเธรด . คุณสามารถกำหนดจำนวนผู้ปฏิบัติงานที่จะดำเนินการตามคำขอได้ การใช้มากกว่า (เช่น 32) มากกว่า 4 จะทำให้โค้ดของคุณเร็วขึ้นอย่างมาก

import requests
from concurrent.futures import ThreadPoolExecutor


NUM_OF_WORKERS=32


def check(username):
    try:
        req = requests.get("http://kik.me/"+username, allow_redirects=False).status_code

        if req == 302:
            print(username, " does not exist.")
        if req == 200:
            print(username, "exists.")
    except Exception as error:
        print(error)


usernames = _loadList(filename)

with ThreadPoolExecutor(max_workers=NUM_OF_WORKERS) as pool:
    pool.map(check, usernames)

ซึ่งจะทำให้โค้ดของคุณวิธีอ่านง่ายขึ้นเช่นกัน

แก้ไข: สังเกตเห็นแท็ก Python 2.7 แล้ว

Python 2 มีกลุ่มเธรดซึ่งพร้อมใช้งานภายใต้โมดูล multiprocessing น่าเสียดายที่ไม่มีการบันทึกไว้เนื่องจากไม่มีการทดสอบใดๆ

import requests
from multiprocessing.pool import ThreadPool


NUM_OF_WORKERS=32


def check(username):
    try:
       req = requests.get("http://kik.me/"+username, allow_redirects=False).status_code

       if req == 302:
           print(username, " does not exist.")
       if req == 200:
           print(username, "exists.")
    except Exception as error:
        print(error)


usernames = _loadList(filename)


pool = ThreadPool(processes=NUM_OF_WORKERS)
pool.map_async(check, usernames)
pool.close()
pool.join()

หากคุณต้องการ Pool of Threads ที่ดีขึ้นสำหรับ Python 2 คุณสามารถลองใช้ โมดูล Pebble

person noxdafox    schedule 18.10.2015
comment
ฮ่าๆ ฉันทดสอบสคริปต์ด้วยชื่อผู้ใช้แบบสุ่มและได้รับชื่อผู้ใช้ที่มีอยู่แล้ว เช่นบาร์นาวี xD - person noxdafox; 18.10.2015
comment
คำถามติดแท็ก python2.7 - person Padraic Cunningham; 18.10.2015