Python 多处理 Pool.map
Python Multiprocessing Pool.map
我尝试在 python 中使用多处理读取文件。这是一个小例子:
import multiprocessing
from time import *
class class1():
def function(self, datasheetname):
#here i start reading my datasheet
if __name__ == '__main__':
#Test with multiprosessing
pool = multiprocessing.Pool(processes=4)
pool.map(class1("Datasheetname"))
pool.close()
现在我得到以下错误:
TypeError: map() missing 1 required positional argument: 'iterable'
在此版块的另一个帖子中,我得到了使用 ThreadPool 执行此操作的提示,但我不知道该怎么做。有什么想法吗?
map(func, iterable[, chunksize])
A parallel equivalent of the map() built-in function (it supports only one iterable argument though). It blocks until the result is ready.
This method chops the iterable into a number of chunks which it submits to the process pool as separate tasks. The (approximate) size of these chunks can be specified by setting chunksize to a positive integer.
您需要传递一个可迭代对象,其中每个元素都作为每个进程中的参数传递给目标func
。
示例:
def function(sheet):
# do something with sheet
return "foo"
pool = Pool(processes=4)
result = pool.map(function, ['sheet1', 'sheet2', 'sheet3', 'sheet4'])
# result will be ['foo', 'foo', 'foo', 'foo']
我尝试在 python 中使用多处理读取文件。这是一个小例子:
import multiprocessing
from time import *
class class1():
def function(self, datasheetname):
#here i start reading my datasheet
if __name__ == '__main__':
#Test with multiprosessing
pool = multiprocessing.Pool(processes=4)
pool.map(class1("Datasheetname"))
pool.close()
现在我得到以下错误:
TypeError: map() missing 1 required positional argument: 'iterable'
在此版块的另一个帖子中,我得到了使用 ThreadPool 执行此操作的提示,但我不知道该怎么做。有什么想法吗?
map(func, iterable[, chunksize])
A parallel equivalent of the map() built-in function (it supports only one iterable argument though). It blocks until the result is ready.
This method chops the iterable into a number of chunks which it submits to the process pool as separate tasks. The (approximate) size of these chunks can be specified by setting chunksize to a positive integer.
您需要传递一个可迭代对象,其中每个元素都作为每个进程中的参数传递给目标func
。
示例:
def function(sheet):
# do something with sheet
return "foo"
pool = Pool(processes=4)
result = pool.map(function, ['sheet1', 'sheet2', 'sheet3', 'sheet4'])
# result will be ['foo', 'foo', 'foo', 'foo']