【问题标题】:Why does the ProcessPoolExecutor ignore the max_workers argument?为什么 ProcessPoolExecutor 忽略 max_workers 参数?
【发布时间】:2015-12-22 00:28:02
【问题描述】:

我预计将 max_workers 参数设置为 1 的 apscheduler.executors.pool.ProcessPoolExecutor 不会并行执行多个作业。

import subprocess

from apscheduler.executors.pool import ProcessPoolExecutor
from apscheduler.schedulers.blocking import BlockingScheduler


def run_job():
    subprocess.check_call('echo start; sleep 3; echo done', shell=True)

scheduler = BlockingScheduler(
        executors={'processpool': ProcessPoolExecutor(max_workers=1)})

for i in range(20):
    scheduler.add_job(run_job)
scheduler.start()                                

但实际上最多可以并行执行十个作业。

我误解了这个概念还是这是一个错误?

【问题讨论】:

  • 您是否偶然创建了多个这样的执行程序?查看代码可能会有所帮助。
  • 你需要举一个更完整的例子——一个我可以自己运行的例子。一方面,您确定要指定正确的执行程序来运行吗?

标签: python apscheduler


【解决方案1】:

这没有按预期工作的原因是因为您没有指定要在哪个执行器中运行作业。

试试这个:

for i in range(20):
    scheduler.add_job(run_job, executor='processpool')

【讨论】:

  • 非常感谢您的帮助!是否可以阻止Scheduler添加默认执行器?
  • 不,也不应该。但是您可以将自己的执行程序定义为“默认”。
  • 您愿意详细说明一下吗?显然我对 apscheduler 的概念有一些误解。
  • 您将执行程序命名为“processpool”,但如果您将其命名为“default”,它将默认使用,而无需在 add_job() 中明确指定名称。
猜你喜欢
  • 1970-01-01
  • 2014-07-08
  • 1970-01-01
  • 2020-03-11
  • 1970-01-01
  • 1970-01-01
  • 2015-12-15
  • 2011-08-30
  • 1970-01-01
相关资源
最近更新 更多