toy toy - 1 year ago 188
Node.js Question

How to run run python script like pm2 for nodejs

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

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

Here's my error

File "/usr/local/lib/python2.7/dist-packages/tweepy/", line 430, in filter
File "/usr/local/lib/python2.7/dist-packages/tweepy/", line 346, in _start
File "/usr/local/lib/python2.7/dist-packages/tweepy/", line 286, in _run
raise exception
AttributeError: 'NoneType' object has no attribute 'strip'

It's a simple data collecting script

class StdOutListener(StreamListener):

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

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

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'

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

Answer Source

There are several solutions for that. First, you may use 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.

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download