Multithreading in the program does not work for me, what could be the problem?

Question:

I’m trying to make my account autoregister on 1 of the servicers using python. I decided to resort to multithreading. I enter the number of threads I need into the console, but they do not start asynchronously. At first, the first thread finishes its work, then the 2nd one starts, and so on. As a result, such a program works very slowly, how can this be fixed?

<— starting multithreading (Here I split the base of logins into the number of threads, maximum 100/10 = 10 logins per thread and transfer them to the thread itself) —>

    for v in range(int(thread_count)):
        if(lines[v]):
            worker = Thread(target=sendRequest(v, lines[v], proxies, proxy_type))
            threads.append(worker)

    for x in threads:
        x.start()

<— For each line passed to the stream, I create an asynchronous request —>

def sendRequest(th_count, names, proxy, proxy_type):
    for x in names:
            loop = asyncio.new_event_loop()
            asyncio.set_event_loop(loop)
            res = loop.run_until_complete(register(x, proxy, proxy_type, th_count))
            loop.close()

<— Here is the function of sending a get request. —>

async def checking(name, proxy_array, proxy_type, th):
    random_proxy = proxy_array[random.randint(0, len(proxy_array) - 1)].rstrip()
    if proxy_type == 0:
        prox = f'socks4://{random_proxy}'
        proxies = {
            'http': prox,
            'https': prox
        }
    elif proxy_type == 1:
        prox = f'socks5://{random_proxy}'
        proxies = {
            'http': prox,
            'https': prox
        }
    try:
        r = requests.get("https://www.google.com/", proxies=proxies, timeout=3)
        print(f'{th} - {r}')
    except:
        print('err')

I’ve tried everything, but it still works terribly long, I don’t understand what I’m doing wrong. But the threads do not want to run all at the same time

Answers:

You have in part:

            ...
            worker = Thread(target=sendRequest(v, lines[v], proxies, proxy_type))
            ...

The first argument to the Thread initializer should be a reference to the "worker" function, sendRequest in this case, that you want multithreaded and the remaining arguments will be actual arguments passed to your worker function after the thread has been started. The args keyword argument is used to pass an iterable of all the positional arguments you want to pass to your worker function and the kwargs keyword arguments is used to pass a dictionary of keyword argument names and values that will be the keyword arguments passed to your worker function.

What you are doing is passing only a single argument to the Thread initializer that is the return value form calling sendRequest(v, lines[v], proxies, proxy_type) in the current thread. This return value appears to be None looking at the function definition for sendRequest and that is not a legal function reference. So all your start requests will fail. So instead of passing a function reference you are actually calling the function and passing its return value.

TL;DR

What you need is:

            worker = Thread(target=sendRequest, args=(v, lines[v], proxies, proxy_type))
Answered By: Booboo