【发布时间】:2015-06-29 07:21:14
【问题描述】:
我正在使用 Python v 2.2.1 并尝试编写需要在 for 循环内并行执行某些任务的代码。迭代次数不固定,取决于列表(在本例中为serverList)。我如何通过多线程或多处理来实现这一点。我通读了一些示例,但它们并行调用了不同的函数。在这里,我需要调用相同的函数 (shutdownTask),但参数不同。部分代码如下:
try:
<some code>
for server in serverList:
print ('Shutting down server: '+server+'...')
shutdownTask(server, serverType)
finally:
verifyStatus()
【问题讨论】:
-
你试过Pykka吗?
-
Python v 2.2.1?真的吗?因为那是古老的。
-
您使用 13 年前的 python 版本有什么原因吗?
-
查看 print 函数(及其括号),您可能指的是 Python v 3.2.1。虽然我仍然建议升级到当前版本(3.4)。
-
introduction on Pythons multiprocessing 给出了一个非常简单的例子,用不同的参数并行调用相同的函数。您可以根据自己的需要进行调整。
标签: python multithreading for-loop multiprocessing python-2.2