Python:使用 Pool,apply_async 并加入
Python: Working with Pool, apply_async and join
我需要 运行 一个 shell 脚本,它从服务器获取文件并根据传递给它的参数写入磁盘。我需要 运行 这个脚本用于大约 20 多个不同的参数,因此需要为每个参数并行 运行 它。
我尝试在 python 中使用 Pool
、apply_async
和 join
来完成此操作。但这似乎不起作用。下面是代码:
import yaml
from subprocess import call
from multiprocessing import Pool
feeder_server_conf = "/etc/feeder-servers.conf"
with open("feed_conf.yaml", "r") as stream:
feeds = yaml.load_all(stream)
pool = Pool()
for feed in feeds:
for key, value in feed.items():
keep_count_present = False
name = value['name']
age = value['age']
if 'keep-count' in value:
keep_count = value['keep-count']
keep_count_present = True
print "Pulling feed..."
if keep_count_present:
command = "/usr/bin/pull-feed --name " + name + " --config " + feeder_server_conf + " --age " + str(age) + " --keep-count " + str(keep_count)
else:
command = "/usr/bin/pull-feed --name " + name + " --config " + feeder_server_conf + " --age " + str(age)
pool.apply_async(call, command.split())
print "Waiting for pull-feeds to finish..."
pool.close()
pool.join()
该代码仅打印以下输出并退出,而没有提取任何文件。不知道我在这里错过了什么。任何帮助表示赞赏。谢谢!
Pulling feeds...
Pulling feed...
Pulling feed...
Pulling feed...
Pulling feed...
Pulling feed...
Pulling feed...
Pulling feed...
Pulling feed...
Pulling feed...
Pulling feed...
Pulling feed...
Pulling feed...
Pulling feed...
Pulling feed...
Pulling feed...
Pulling feed...
Pulling feed...
Pulling feed...
Pulling feed...
Pulling feed...
Pulling feed...
Waiting for pull-feeds to finish...
我尝试执行的脚本在单独 运行 时工作正常(因此不是罪魁祸首)。我希望代码等到所有文件都被拉出。不知道我在这里错过了什么。我没有以正确的方式使用结构吗?任何帮助表示赞赏。谢谢!
这里的问题很简单,如documentation of subprocess.call所述call
的第一个参数是list,但是apply_async
函数从它的第二个参数应用列表,就像 function(*args)
那样,这意味着您的调用最终看起来像这样 call('/usr/bin/pull-feed', '--name', ...)
,这不是您使用 call
的方式。您需要做的就是将 pool.apply_async(call, command.split())
替换为 pool.apply_async(call, [command.split()])
,将您的命令作为列表传递给 call
的第一个参数,apply_async
使用的最终命令将如下所示这个call(['/usr/bin/pull-feed', '--name'])
.
我需要 运行 一个 shell 脚本,它从服务器获取文件并根据传递给它的参数写入磁盘。我需要 运行 这个脚本用于大约 20 多个不同的参数,因此需要为每个参数并行 运行 它。
我尝试在 python 中使用 Pool
、apply_async
和 join
来完成此操作。但这似乎不起作用。下面是代码:
import yaml
from subprocess import call
from multiprocessing import Pool
feeder_server_conf = "/etc/feeder-servers.conf"
with open("feed_conf.yaml", "r") as stream:
feeds = yaml.load_all(stream)
pool = Pool()
for feed in feeds:
for key, value in feed.items():
keep_count_present = False
name = value['name']
age = value['age']
if 'keep-count' in value:
keep_count = value['keep-count']
keep_count_present = True
print "Pulling feed..."
if keep_count_present:
command = "/usr/bin/pull-feed --name " + name + " --config " + feeder_server_conf + " --age " + str(age) + " --keep-count " + str(keep_count)
else:
command = "/usr/bin/pull-feed --name " + name + " --config " + feeder_server_conf + " --age " + str(age)
pool.apply_async(call, command.split())
print "Waiting for pull-feeds to finish..."
pool.close()
pool.join()
该代码仅打印以下输出并退出,而没有提取任何文件。不知道我在这里错过了什么。任何帮助表示赞赏。谢谢!
Pulling feeds...
Pulling feed...
Pulling feed...
Pulling feed...
Pulling feed...
Pulling feed...
Pulling feed...
Pulling feed...
Pulling feed...
Pulling feed...
Pulling feed...
Pulling feed...
Pulling feed...
Pulling feed...
Pulling feed...
Pulling feed...
Pulling feed...
Pulling feed...
Pulling feed...
Pulling feed...
Pulling feed...
Pulling feed...
Waiting for pull-feeds to finish...
我尝试执行的脚本在单独 运行 时工作正常(因此不是罪魁祸首)。我希望代码等到所有文件都被拉出。不知道我在这里错过了什么。我没有以正确的方式使用结构吗?任何帮助表示赞赏。谢谢!
这里的问题很简单,如documentation of subprocess.call所述call
的第一个参数是list,但是apply_async
函数从它的第二个参数应用列表,就像 function(*args)
那样,这意味着您的调用最终看起来像这样 call('/usr/bin/pull-feed', '--name', ...)
,这不是您使用 call
的方式。您需要做的就是将 pool.apply_async(call, command.split())
替换为 pool.apply_async(call, [command.split()])
,将您的命令作为列表传递给 call
的第一个参数,apply_async
使用的最终命令将如下所示这个call(['/usr/bin/pull-feed', '--name'])
.