【问题标题】:python: get callback from linuxpython:从linux获取回调
【发布时间】:2013-11-23 19:51:39
【问题描述】:

我在 Linux 上的 phantomJS 中有一个脚本,用于呈现示例网站:

var page = require('webpage').create();
page.open('http://www.example.com', function () {
    page.render('test.pdf');
    console.log('pdf render finished')
    phantom.exit();
});

我从 python 调用它:

os.system('phantomjs test.js')

我想要的是让 Python 知道 pdf 渲染已经完成的方法;类似于“pdf 渲染完成”这一行的监听器,或者类似这些行的东西。像这样的回调/侦听器将如何构建以及如何实现?

【问题讨论】:

标签: python callback listener phantomjs


【解决方案1】:

您可以像这样使用子流程模块:

returncode =  subprocess.call(["phantomjs", "test.js"])

if returncode == 0:
   print "Application successfull executed"
else:
   print "There was an Error"

【讨论】:

  • 这相当于他的os.system() 示例。我相信他打算让这个过程长期运行,因此依赖状态码不是一种选择。
【解决方案2】:

os.system 只返回进程返回码。如果您想与进程交互,包括读取它的 STDOUT,请使用popen。您可以在您的进程和 phantomjs 之间创建一个 PIPE,然后等待“pdf 渲染完成”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-01
    • 2014-03-01
    • 1970-01-01
    相关资源
    最近更新 更多