A
A
Akshin Yolchuev2020-04-13 17:58:23
Python
Akshin Yolchuev, 2020-04-13 17:58:23

Distributing tasks to Python threads?

Hello!
At me the following task:
I connect 100 flows and I throw the general file with lines.
I need to make sure that the threads do not repeat each other and bring the matter to the end,
my solution is:

from threading import Thread as th


def bot():
    global baza

    for i in baza:


        bi = i
        baza.remove(i)

        
        





baza = open('baza.txt','r').read().split()
for i in range(30):
    th(target=bot).start()

with this code, each thread removes its value from the array, because of this, other threads cannot take it.
How can you do it differently?
the problem is that when i do this, then for some problem i can't restart the task with the same value as it got deleted

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2020-04-13
@Akshin17

Everything has already been thought of for you. The standard library has a thread pool, to which you can pass a function, an array of data and the size of a piece processed by one thread, and then it will create threads and distribute data to them.

D
Dmitry Shitskov, 2020-04-16
@Zarom

Use a Queue instead of an array

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question