How to take multiple arguments when doing parallel processing using multiprocessing in python

Sample code

sample.py


#!/usr/bin/env python
# -*- coding: utf-8 -*-
from multiprocessing import Pool
from collections import Counter

#Parallel processing here
def func(n, argument1, argument2):
    #The process of doubling and adding 5
    return n * argument1 + argument2

def wrapper(args):
    #args(i, 2, 5)Has become
    return func(*args)

def multi_process(sampleList):
    #Number of processes:8(Parallel processing with 8 cpu)
    p = Pool(8)
    output = p.map(wrapper, sampleList)
    #End of process
    p.close()
    return output

if __name__ == "__main__":
    #Perform 100 processes in parallel
    num = 100

    # (i, 2, 5)Is the argument
    sampleList = [(i, 2, 5) for i in range(num)]

    #Double the elements of sampleList and add 5
    output = multi_process(sampleList)

Description

The python version is 2.7.10. You can see it by looking at the code

-When parallel processing using ** multiprocessing , it is easy to pass it as a list. - wrapper () ** You can pass multiple arguments by putting a function in between. ――In this sample, all the first elements of the list are doubled and 5 is added. --If you forget to close the process (** Pool.close () **), you may open the process too much and get the error Too many open files.

Recommended Posts

How to take multiple arguments when doing parallel processing using multiprocessing in python
How to exit when using Python in Terminal (Mac)
How to retrieve multiple arrays using slice in python.
Things to watch out for when using default arguments in Python
To receive multiple return values ​​from a function executed using parallel processing (Pool) in Python
How to write a string when there are multiple lines in python
How to receive command line arguments in Python
How to do multi-core parallel processing with python
How to plot multiple fits images side by side in galactic coordinates using python
How to write string concatenation in multiple lines in Python
How to measure processing time in Python or Java
How to execute a command using subprocess in Python
Things to keep in mind when processing strings in Python2
Things to keep in mind when processing strings in Python3
How to develop in Python
How to host web app backend processing in Python using a rental server subdomain
Using Python mode in Processing
How to slice a block multiple array from a multiple array in Python
How to define multiple variables in a python for statement
How to specify command line arguments when debugging in PyCharm
Things to keep in mind when using Python with AtCoder
Things to keep in mind when using cgi with python.
Parallel processing of Python joblib does not work in uWSGI environment. How to process in parallel on uWSGI?
How to install python using anaconda
[Python] How to do PCA in Python
Python parallel processing (multiprocessing and Joblib)
Precautions when using pit in Python
How to collect images in Python
How to use SQLite in Python
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
How to handle Japanese in Python
When using regular expressions in Python
How to not escape Japanese when dealing with json in python
How to pass arguments to a Python script in SPSS Modeler Batch
How to display formulas in latex when using sympy (> = 1.4) in Google Colaboratory
How to delete multiple specified positions (indexes) in a Python list
Precautions when giving default values to arguments in Python function definitions
When you want to replace multiple characters in a string without using regular expressions in python3 series
When specifying multiple keys in python sort
How to access environment variables in Python
Periodic execution processing when using tkinter [Python3]
How to dynamically define variables in Python
How to hide the command prompt when running python in visual studio 2015
How to do R chartr () in Python
[Itertools.permutations] How to put permutations in Python
How to deal with OAuth2 error when using Google APIs from Python
Send email to multiple recipients in Python (Python 3)
[python, multiprocessing] Behavior for exceptions when using multiprocessing
When running a Python shell from Electron, pass multiple arguments to run Python.
How to work with BigQuery in Python
Log in to Slack using requests in Python
How to get a value from a parameter store in lambda (using python)
How to get a stacktrace in python
How to display multiplication table in python
How to extract polygon area in Python
How to check opencv version in python
How to plot galaxy visible light data using OpenNGC database in python
[Introduction to Python] How to delete rows that meet multiple conditions in Pandas.DataFrame