简体   繁体   English

如何将Python多重处理用于位置参数为零的函数?

[英]How do you use Python Multiprocessing for a function with zero positional arguments?

Here is an example: 这是一个例子:

import multiprocessing
def function():
    for i in range(10):
        print(i)

if __name__ == '__main__':
    p = multiprocessing.Pool(5)
    p.map(function, )

yields the error: TypeError: map() missing 1 required positional argument: 'iterable' 产生错误: TypeError: map() missing 1 required positional argument: 'iterable'

The function does not need any input, so I wish to not artificially force it to. 该功能不需要任何输入,因此我希望不要人为地强迫它。 Or does multiprocessing need some iterable? 还是多处理需要一些迭代?

The following code returns / prints nothing. 以下代码不返回/不输出任何内容。 Why? 为什么?

import multiprocessing
def function():
    for i in range(10):
        print(i)

if __name__ == '__main__':
    p = multiprocessing.Pool(5)
    p.map(function, ())

If you have no arguments to pass in, you don't have to use map . 如果没有要传递的参数,则不必使用map You can simply use multiprocessing.Pool.apply instead: 您可以简单地使用multiprocessing.Pool.apply代替:

import multiprocessing


def function():
    for i in range(10):
        print(i)

if __name__ == '__main__':
    p = multiprocessing.Pool(5)
    p.apply(function)

If you are only trying to perform a small number of tasks, it may be better to use Process for reasons described here . 如果您仅尝试执行少量任务,则出于此处所述的原因,最好使用Process

This site provides an excellent tutorial on use of Process() which i have found helpful. 该站点提供了有关使用Process()的出色教程,我发现它很有帮助。 Here is an example from the tutorial using your function() : 这是使用您的function()的教程示例:

import multiprocessing
def function():
    for i in range(10):
        print(i)

if __name__ == '__main__':
    jobs = []
    for i in range(5):
        p = multiprocessing.Process(target=function)
        jobs.append(p)
        p.start()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 您如何在 Python 中使用多处理并传递多个 arguments 同时还知道正在发生的进程数? - How do you use multiprocessing in Python and pass multiple arguments while also knowing which number of process is occuring? 如何在 Python 的函数中清除位置 arguments? - How do I clear positional arguments in my function(s) in Python? Python 函数缺少位置参数 - Python function missing positional arguments 如何在python中应用涉及两个变量的函数-位置参数问题 - How to apply a function that involves two variables in python - positional arguments issue 我如何在 python 多处理池 apply_async 中使用关键字参数 - how do I use key word arguments with python multiprocessing pool apply_async 你如何对 Python 中的函数参数施加限制? - How do you impose restrictions on function arguments in Python? 如何透明地将参数传递给python中的函数? - How do you transparently pass arguments through to a function in python? 查找导数时,在python 3中,如何使用filter函数仅返回导数未乘以零的项? - When finding derivatives, how do you use the filter function to return only the terms whose derivatives are not multiplied by zero, in python 3? 多处理:如何在列表上使用pool.map并使用参数函数? - Multiprocessing: How to use pool.map on a list and function with arguments? 为什么在 python 3.8 中只使用位置 arguments? - Why use positional only arguments in python 3.8?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM