Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String arguments in python multiprocessing

I'm trying to pass a string argument to a target function in a process. Somehow, the string is interpreted as a list of as many arguments as there are characters.

This is the code:

import multiprocessing  def write(s):    print s  write('hello')  p = multiprocessing.Process(target=write, args=('hello'))  p.start() 

I get this output:

hello Process Process-1: Traceback (most recent call last): >>>   File "/usr/local/lib/python2.5/site-packages/multiprocessing/process.py", line 237, in _bootstrap     self.run()   File "/usr/local/lib/python2.5/site-packages/multiprocessing/process.py", line 93, in run     self._target(*self._args, **self._kwargs) TypeError: write() takes exactly 1 argument (5 given)  >>> 

What am I doing wrong? How am I supposed to pass a stringn?

Thanks, Ariel

like image 328
abalter Avatar asked Oct 13 '09 09:10

abalter


People also ask

How do you pass arguments in multiprocessing in Python?

Passing Keyword Arguments to Multiprocessing Processes We can also pass in arguments corresponding to the parameter name using the kwargs parameter in the Process class. Instead of passing a tuple, we pass a dictionary to kwargs where we specify the argument name and the variable being passed in as that argument.

What is pool in multiprocessing Python?

The Pool class in multiprocessing can handle an enormous number of processes. It allows you to run multiple jobs per process (due to its ability to queue the jobs). The memory is allocated only to the executing processes, unlike the Process class, which allocates memory to all the processes.

What is Pool map in Python?

Pool process pool provides a version of the map() function where the target function is called for each item in the provided iterable in parallel. A parallel equivalent of the map() built-in function […]. It blocks until the result is ready. — multiprocessing — Process-based parallelism.


1 Answers

This is a common gotcha in Python - if you want to have a tuple with only one element, you need to specify that it's actually a tuple (and not just something with brackets around it) - this is done by adding a comma after the element.

To fix this, just put a comma after the string, inside the brackets:

p = multiprocessing.Process(target=write, args=('hello',)) 

That way, Python will recognise it as a tuple with a single element, as intended. Currently, Python is interpreting your code as just a string. However, it's failing in this particular way because a string is effectively list of characters. So Python is thinking that you want to pass ('h', 'e', 'l', 'l', 'o'). That's why it's saying "you gave me 5 parameters".

like image 152
Smashery Avatar answered Oct 15 '22 04:10

Smashery