Python MultiProcessing.Pool : How to use with no iterable?

标签 python multithreading python-3.x multiprocessing python-multiprocessing

多线程进程

    p = ThreadPool(processes=10)  # creates a pool of 10 workers
    p.map(function_to_call, iterable)  # calls FUNCTION_TO_CALL with the first item from iterable as parameter
    p.close()  # closes the multi-threaded processes one all threads done

我一直在尝试使用这个模型,但是如果我想线程化一个没有参数的函数怎么办。像 run() 。我会为“可迭代”空间放什么,我一直在四处寻找,但找不到解决方案。

最佳答案

Pool.apply 功能就是你要找的。使用 Pool.apply_async如果你想要一个非阻塞调用。

p = ThreadPool(processes=10)
p.apply(function_to_call)
p.close()

关于Python MultiProcessing.Pool : How to use with no iterable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43565035/

相关文章:

带有 Boss-Worker 的 Java 线程

python-3.x - Flask 应用程序在部署到 Heroku 时出错 at=error code=H10 desc ="App crashed"method=GET path ="/"

python - 使用 Python 提交表单

python - 如何正确使用matplotlib渲染图像直方图?

android - 在写入文件时读取文件

java - 多线程场景下的数据可见性

函数的 Python 变量参数

windows - cx_freeze include_msvcr 不捆绑 windows VC2015 runtime

python - 使用 python 将两个文件中的每一行连接为一行

python - 如果后续列表包含相同元素,则删除所有先前列表