D
D
DTPlayer2021-07-21 03:16:50
Python
DTPlayer, 2021-07-21 03:16:50

How to solve the problem with multithreading?

def new_therad(eth_list, all_string, count):
    list_all = []
    for i in range(len(eth_list)):
        if eth_list[i] in blacklist:
            continue
        list_all.append(Thread(target=get_data, args=(eth_list[i], all_string[i])))
        blacklist.append(eth_list[i])
        if len(list_all) == count:
            print('Собрано нужное количество потоков, запуск')
            for i in range(len(list_all)):
                loop.run_until_complete(run_thread(list_all[i], i+1))
        if len(blacklist) == MainFileLen.count:
            pass
        else:
            new_therad(eth_list, all_string, count)

Here is the function, blacklist is initially empty
async def run_thread(thread, num):
    thread.start()
    thread.join()
    print(f'Поток номер {num} завершен')

At the first start, everything is fine, but on the second request of the function, it starts adding 100+ threads, as a result, an error about the lack of entries in the array. In multithreading recently, I would like to know how to solve this.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DTPlayer, 2021-07-21
@DTPlayer

Thanks to those who helped, now the code looks like this:

def new_therad(eth_list, all_string, count):
    for i in range(len(eth_list)):
        if threading.active_count() == count-1:
            time.sleep(1)
        flow1 = Thread(target=get_data, args=(eth_list[i], all_string[i]), daemon=True)
        flow1.start()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question