【问题标题】:Can't execute Ruby Script while running Python Celery as Daemon在将 Python Celery 作为守护进程运行时无法执行 Ruby 脚本
【发布时间】:2016-01-01 14:05:53
【问题描述】:

我有 Python Celery 作为守护程序运行,Celerybeat 在 Amazon Linux 机器上接收任务。当我运行我的测试任务时,它会顺利完成。

@app.task
def test():
    print("Testing 123")
    return 0

但是,当我尝试使用 Python 的 subprocess 模块启动 Ruby 脚本时,它会轮询几次,然后以 1 的返回码退出。

@app.task
def run_cli():
    try:
        process = subprocess.Popen([filepath, "run"], stdout=subprocess.PIPE)

        while not process.poll():
            data = process.stdout.readline()
            if data:
                sys.stdout.write("Polling: " + data.decode("utf-8"))
            else:
                sys.stdout.write("Polling: No Data.")

        return process.wait()
    except Exception as e:
        print(e)

我已经确认,当 Celery 工作人员在 Python shell 中使用 tasks.run_cli.apply() 执行 Ruby 脚本时,它可以干净地运行。那么为什么 Celery Daemon 不执行这些任务呢?

预警:我对 Python 和 Celery 还很陌生,而且我的 Linux 技能参差不齐,所以如果有明显问题,我深表歉意。非常感谢任何帮助。

【问题讨论】:

    标签: ruby linux celery daemon python-3.5


    【解决方案1】:

    为了结束轮询,Ruby 脚本必须向 Python 脚本发送一个非零信号,否则该过程完成并且 Python 继续轮询而不接收数据。也可以在 else 条件下跳出循环,因为对 process.stdout.readline() 的错误响应表明(我怀疑)脚本处理已完成。

    【讨论】:

      猜你喜欢
      • 2014-04-26
      • 2012-11-23
      • 2011-01-03
      • 2013-10-14
      • 1970-01-01
      • 1970-01-01
      • 2015-06-02
      相关资源
      最近更新 更多