Luan Devecchi
Luan Devecchi

Reputation: 3

Multi-Threading Function

I'm using Python 3 to send a request to my API.

I read a file and for each line I use the function

lista = open('mylist.txt', 'r').readlines()
lista = [dab.replace('\n', '') for dab in lista]

for dab in lista:
    sdab = dab.split(':')
    login = sdab[0]
    senha = sdab[1]
    myfunction(login,senha)

myfunction just does a request to my api.

is it possible, for example, after taking the number of rows from the file to make threads and send the contents of each line to the function?

Upvotes: 0

Views: 49

Answers (1)

Flight Odyssey
Flight Odyssey

Reputation: 2287

Yes, this is quite straightforward with the multiprocessing package.

You'll need a function that can be called directly on each dab in lista, something like:

def dabify(dab):
    sdab = dab.split(':')
    login = sdab[0]
    senha = sdab[1]
    myfunction(login,senha)

Then, you can use a multiprocessing pool to call dabify on each element of lista:

from multiprocessing import Pool
pool = Pool(processes=4) # specify number of processes
for result in pool.imap_unordered(dabify, lista):
    # do something with the result here if you want
    pass

Note that the order that the API gets called is no longer guaranteed.

Upvotes: 1

Related Questions