How to use multithreading with requests?

Asked

Viewed 457 times

0

Hello, I’m developing an availability Hecker to change names for a game, but it is very slow, I read about the multithreading module but I found it confusing to use, I have no idea how to implode it in my code, someone can show me the way?

Here my code

import faster_than_requests as requests
from bs4 import BeautifulSoup


available_summoners = []
summonertxt = [line.rstrip('\n') for line in open("names.txt", 'r')]


for summoner in summonertxt:
    url = 'http://www.lolnames.gg/pt/br/'+summoner
    request = requests.get2str(url)
    html = BeautifulSoup(request,"lxml")
    if 'card bg-success text-white' in request:
        print(summoner +' Disponivel!')
    else:
        print(summoner +' Não Disponivel')

  • the page when accessing the url http://www.lolnames.gg/pt/br/ returns Bad Request (400), this txt is extra compos or an attempt to answer 200 ?

1 answer

0

To accomplish this task you can create a Thread object using the module threading.

To create a Thread, you must pass the function to be called in the parameter target and then you can pass the arguments of this function in the parameter args inside a tuple or list.

After creating the object, run the thread with the method start. See the example below:

def func(url):
    print("Obtendo a resposta de", url)

Thread(target = func, args = ("<sua_url>", )).start()

Now that you know how to create a Thread and run it, just put your code inside a function to run in a Thread. See below how your code can look:

from threading import Thread
import faster_than_requests as requests

def validate_summoner(url):

    request = requests.get2str(url)

    if 'card bg-success text-white' in request:
        print(summoner + ' Disponivel!')      
    else:
        print(summoner + ' Não Disponivel')  


available_summoners = []
summonertxt = [line.rstrip('\n') for line in open("names.txt")]

for summoner in summonertxt:
    url = 'http://www.lolnames.gg/pt/br/' + summoner
    Thread(target = validate_summoner, args = (url, )).start()
  • It’s still slow, so I can increase the number of threads in my code?

  • Perhaps this slowness is the fault of the server, so as to this can not do anything if you are not the owner of the server. And there’s no way to create new threads in this case, because each thread is responsible for a request in the url with a summoner. Therefore, the number of threads created there in the code is the number of Summoners you have in the list summonertxt.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.