如何为nodejs运行像pm2这样的python脚本 [英] How to run a python script like pm2 for nodejs

查看:104
本文介绍了如何为nodejs运行像pm2这样的python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've used pm2 for my Node.js script and I love it.
Now I have a python script which collect streaming data on EC2. Sometimes the script bombs out and I would like a process manager to restart itself like pm2.

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

Is there something the same as pm2 for python? I've been searching around and couldn't find anything.

这是我的错误

  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:

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

It's a simple data collecting script

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=[''])

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

That I would like it to just restart itself in case something happens.

推荐答案

UPD:请参阅下面的答案以获得更好的解决方案.

UPD: See answers below for better solutions.

--

对此有多种解决方案.首先,您可以使用 http://supervisord.org/ 这是一个不错的通用过程控制系统,其中包括很多开箱即用的功能,例如自动重启、重启计数器、日志记录、灵活配置等.

There are several solutions for that. First, you may use http://supervisord.org/ which is a decent universal process controll system, which includes a lot of features out of the box, such as autorestart, restart counter, logging, flexible configuration and more.

除此之外,您可以将实现逻辑包装到一个函数中,在 try except 块中运行它,捕获所有异常,并在出现异常时再次运行该函数而不是退出脚本.在您的情况下,此类功能可能包括创建侦听器、身份验证和流部分.

Beyond that, you may just wrap your implementation logic into a function, run it within try except block, catch all exceptions and when an exception is cought, run the function again instead of exiting the script. In your case such function might include creating listener, authentication and stream part.

这篇关于如何为nodejs运行像pm2这样的python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆