I was trying to do some multiprocessing.
import multiprocessing
with multiprocessing.Pool() as pool:
pool.apply_async(func,('some string'))
I get an error:
TypeError: func() takes 1 positional arguments but 261 were given
This is very suspicious because the string I was using as the argument had 260 characters.
Does anyone know why this is happening and how to solve it?
According to Python docs (emphasis mine!),
A special problem is the construction of tuples containing 0 or 1 items: the syntax has some extra quirks to accommodate these. Empty tuples are constructed by an empty pair of parentheses; a tuple with one item is constructed by following a value with a comma (it is not sufficient to enclose a single value in parentheses). Ugly, but effective.
So, the expression ('a')
is actually creating a string of value 'a'
, but the expression ('a',)
is creating a tuple of a single string element 'a'
.
That said, you can refactor your code as
import multiprocessing
with multiprocessing.Pool() as pool:
pool.apply_async(func, ('some string',))