【问题标题】:How to make Hubot on IRC print external script output as it is received instead of all at once?如何让 IRC 上的 Hubot 打印外部脚本输出,而不是一次全部打印?
【发布时间】:2015-07-15 06:36:44
【问题描述】:

我有一个在 IRC 上运行的 Hubot 实例。在我的脚本目录中,我有几个咖啡脚本链接到外部 python 脚本。我的问题是我的数据应该在外部脚本中的其他数据之前打印出来,例如“处理请求。请稍候......”等,Hubot 等待整个脚本完全完成执行并立即转储 IRC 的输出.

如何修改我的咖啡脚本以在收到外部脚本时发送输出?

咖啡脚本示例:

# Commands:
#   Hubot jira-add-comment <ticket> "comment" - Add given comment to a JIRA ticket

{spawn} = require 'child_process'
module.exports = (robot) ->

addComment = (msg,ticket,comment) -> 
    output = spawn "/path/to/externalscript.py", [ticket,comment]
    output.stdout.on 'data', (data) ->
        msg.send data.toString()

robot.respond /jira-add-comment (\w+-\d+) (.+)$/i, (msg) ->
    addComment(msg,msg.match[1].trim(),msg.match[2])

谢谢!

【问题讨论】:

  • 我对此也很好奇。如果它可以“缓存”所有内容,那就太好了。

标签: python coffeescript irc hubot


【解决方案1】:

我遇到了同样的问题。解决方案是在从 Hubot 调用 Python 脚本时将“-u”标志传递给 Python。

s = spawn 'python', ['-u', '/your/script/here.py', 'any_other_flags']

https://unix.stackexchange.com/questions/182537/write-python-stdout-to-file-immediately

【讨论】:

    【解决方案2】:

    可能是因为您的示例脚本中的标识错误?

    output.stdout.on 'data', (data) ->
    msg.send data.toString()
    

    应该是:

    output.stdout.on 'data', (data) ->
        msg.send data.toString()
    

    【讨论】:

    • 抱歉,这是我在复制和粘贴时出错。咖啡脚本正确缩进。我已经更新了上面的代码。
    猜你喜欢
    • 2016-09-07
    • 1970-01-01
    • 1970-01-01
    • 2019-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-21
    • 1970-01-01
    相关资源
    最近更新 更多