简体   繁体   中英

Python Multiprocessing function

I have a function for example:

launcher(usr_login, usr_password)

which calls other python script + usr_login + usr_password as arguments.

See: Function

launcher("login", "pass")

will execute function.py file as $function.py login pass
Subject: I have dict user_login : password, And I would like to be able launch a 'launcher' function in one time multithreaded / multiprocessed

dict has login1 : pass1 , login2 : pass2 So I need launch launcher(login1, pass1) and launcher(login2, pass2) in same time. Is there a way how to do it? Thank you

# -*- coding: utf-8 -*-
from config import users, ThCount
from time import sleep
from multiprocessing import Pool
import os

users = {}

def launcher(usr_login, usr_password):
    os.system("C:\\Python34\\python.exe implementation.py %s %s" % (usr_login, usr_password))

Reply for comment #1 If I use like this:

def launcher(usr_login, usr_password):
    os.system("C:\\Python34\\python.exe implementation.py %s %s" % (usr_login, usr_password))
if __name__ == '__main__':
    with Pool(5) as p:
        p.map(launcher, users)

I got:

TypeError: launcher() missing 1 required positional argument: 'usr_password'

You can't pass multiple arguments to function using Pool.map() .

As a simple solution you can pack them into tuple.

# -*- coding: utf-8 -*-
from multiprocessing import Pool
import os

users = {
    'a': '1',
    'b': '2',
    'c': '3'
}

def launcher(args):
    os.system("python implementation.py %s %s" % (args[0], args[1]))


if __name__ == '__main__':
    with Pool(3) as p:
        p.map(launcher, users.items())

UPD I noticed you're using Python 3.4. Starting from version 3.3 you can use Pool.starmap to pass multiple parameters and therefore keep list of parameters readable as before.

# -*- coding: utf-8 -*-
from multiprocessing import Pool
import os

users = {
    'a': '1',
    'b': '2',
    'c': '3'
}

def launcher(usr_login, usr_password):
    os.system("python implementation.py %s %s" % (usr_login, usr_password))


if __name__ == '__main__':
    with Pool(3) as p:
        p.starmap(launcher, users.items())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM