How to run a python script like pm2 for nodejs

2024/11/20 1:23:49

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.

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/streaming.py", line 430, in filterself._start(async)File "/usr/local/lib/python2.7/dist-packages/tweepy/streaming.py", line 346, in _startself._run()File "/usr/local/lib/python2.7/dist-packages/tweepy/streaming.py", line 286, in _runraise 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 Truedef 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 APIl = 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.

Answer

You can actually run python scripts from within pm2:

pm2 start echo.py

If the script ends in a .py suffix it will use a python interpreter by default. If your filename doesn't end in .py you can do:

pm2 start echo --interpreter=python

I've found you have to be a little bit careful which python you are using, especially if you are using a virtualenv python with a different version to the 'default' python on your machine.

https://en.xdnf.cn/q/26370.html

Related Q&A

Resize rectangular image to square, keeping ratio and fill background with black

Im trying to resize a batch of grayscale images that are 256 x N pixels (N varies, but is always ≤256). My intention is to downscale the images.The resize would have to output a square (1:1) image, wi…

How to capture arbitrary paths at one route in FastAPI?

Im serving React app from FastAPI by mounting app.mount("/static", StaticFiles(directory="static"), name="static")@app.route(/session) async def renderReactApp(request: Re…

Changing the active class of a link with the twitter bootstrap css in python/flask

I got the following html snippet from my page template.html.<ul class=nav><li class="active"><a href=/>Home</a></li><li><a href=/lorem>Lorem</a>…

overwriting a spark output using pyspark

I am trying to overwrite a Spark dataframe using the following option in PySpark but I am not successfulspark_df.write.format(com.databricks.spark.csv).option("header", "true",mode=…

How to patch a constant in python

I have two different modules in my project. One is a config file which containsLOGGING_ACTIVATED = FalseThis constant is used in the second module (lets call it main) like the following:if LOGGING_ACTI…

Python script for minifying CSS? [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.We don’t allow questi…

Saving numpy array to txt file row wise

I have an numpy array of forma = [1,2,3]which I want to save to a .txt file such that the file looks like:1 2 3If I use numpy.savetxt then I get a file like:1 2 3There should be a easy solution to this…

I want Python argparse to throw an exception rather than usage

I dont think this is possible, but I want to handle exceptions from argparse myself.For example:import argparse parser = argparse.ArgumentParser() parser.add_argument(--foo, help=foo help, required=Tru…

Pass Variable from python (flask) to HTML in render template?

The web server works (python flask) but when I go to the website, where the value of animal should be (dog) it shows the variable name animal. (There is more to the code but this is the most simplistic…

How to clear console in sublime text editor

How to clear console in sublime text editor. I have searched on internet too..But cant find proper shortcut for that. Please provide info