Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiprocessing, Pool.map()

I don't understand how it works. I have function log_in with two arguments user and password. And have list with all my users and passwords. So, when I using p.map(log_in, list), it's means that list will iterate and "unpack". Where ['user','bitnami'], ['user1', '12345'] etc will be these arguments log_in(user, password). Yes?

def log_in(user, password):
        payload = wrap_creds_in_xml(username=user, password=password)
        response = requests.post(TARGET_URL, payload)

if __name__ == '__main__':
    TARGET_URL = 'http://192.168.1.6/wp-login.php'
    list = [['user', 'bitnami'], ['user1', '12345'],
            ['user2', '54321'], ['user3', 'qwerty']]
    p = Pool(5)
    p.map(log_in, list)

And I have error

TypeError: main() missing 1 required positional argument: 'password'

like image 352
Leonardo de Python Avatar asked Oct 16 '17 15:10

Leonardo de Python


1 Answers

The problem is that log_in is a function that takes two arguments, but your code passes just a single argument to that function: a list with two elements. Try Pool.starmap instead of Pool.map:

p.starmap(log_in, list)

From the documentation of Pool.starmap():

Like map() except that the elements of the iterable are expected to be iterables that are unpacked as arguments.

Hence an iterable of [(1,2), (3, 4)] results in [func(1,2), func(3,4)].

like image 195
kfx Avatar answered Oct 25 '22 13:10

kfx