python - 如何为nodejs运行像pm2这样的python脚本

标签 python node.js python-2.7 ubuntu process-management

我已将 pm2 用于我的 Node.js 脚本,我喜欢它。
现在我有一个 python 脚本,可以收集 EC2 上的流数据。有时脚本会爆炸,我希望进程管理器像 pm2 一样重新启动自己。

有没有和python的pm2一样的东西?我一直在四处寻找,找不到任何东西。

这是我的错误

  File "/usr/local/lib/python2.7/dist-packages/tweepy/streaming.py", line 430, in filter
    self._start(async)
  File "/usr/local/lib/python2.7/dist-packages/tweepy/streaming.py", line 346, in _start
    self._run()
  File "/usr/local/lib/python2.7/dist-packages/tweepy/streaming.py", line 286, in _run
    raise exception
AttributeError: 'NoneType' object has no attribute 'strip'
/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/util/ssl_.py:90:

这是一个简单的数据收集脚本

class StdOutListener(StreamListener):

    def on_data(self, data):
        mydata = json.loads(data)
        db.raw_tweets.insert_one(mydata)
        return True

    def on_error(self, status):
        mydata = json.loads(status)
        db.error_tweets.insert_one(mydata)


if __name__ == '__main__':

    #This handles Twitter authetification and the connection to Twitter Streaming API
    l = StdOutListener()
    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    stream = Stream(auth, l)

    #This line filter Twitter Streams to capture data by the keywords: 'python', 'javascript', 'ruby'
    stream.filter(follow=[''])

我希望它在发生某些事情时重新启动。

最佳答案

您实际上可以在 pm2 中运行 python 脚本:

pm2 start echo.py

如果脚本以 .py 后缀结尾,它将默认使用 python 解释器。如果您的文件名不以 .py 结尾,您可以这样做:

pm2 start echo --interpreter=python

我发现你必须小心你使用的是哪个 python,特别是如果你使用的 virtualenv python 与你机器上的“默认”python 版本不同。

关于python - 如何为nodejs运行像pm2这样的python脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32127834/

相关文章:

javascript - Node/express 中 require ('module' )() 和 const mod = require ('module' ) mod() 之间的区别

python - 遍历列表返回负索引

python - 抑制复合假设策略的 HealthCheck.too_slow

python - 加快读取python中的url响应

python - 如何为pytest调用其他类python中的函数

python - 在 python 2 或 python 3 中编写 csv 文件的便携方式

Python - 从可能返回 None 的函数中设置变量的安全且优雅的方法

python a[1 :] 的 javascript 简写

node.js - 无法使用 fs.removeSync() 删除文件

node.js - 如何使用csv-parse的读写流