8

I have just changed from Express with NodeJS to Django with Python. The only thing I miss about NodeJS was the wonderful process manager pm2. Can I use pm2 with Django?

davidism
  • 121,510
  • 29
  • 395
  • 339
ruttydm
  • 671
  • 2
  • 8
  • 15

4 Answers4

10

You can define a pm2 script e.g

pm2{name}.json

with the following contents:

 {
  "apps": [
    {
      "name": "{name}",
      "cwd": "/srv/{name}",
      "args": "runserver",
      "script": "manage.py",
      "exec_mode": "fork",
      "exec_interpreter": "python"
    }
  ]
}

and run it with pm2 start {name}

obviously you need to replace {name} with your project's name.

Richard Miles
  • 557
  • 4
  • 18
  • 1
    this is run by using manage.py runserver, how can one create a config that runs gunicorn? – Fed Sep 20 '21 at 08:37
3

Steps

  • Install Node from given link
  • Install npm using this command in command prompt / terminal reference
npm install npm@latest -g
  • Run this command for execute django application non stop on server machine
     pm2 start echosystem.config.json
     Description of echosystem.config.json
{       
   apps:            
      [{
        name: "djnago_with_pm2",          
        script: "manage.py",
        args: ["runserver", "127.0.0.1:8000"], 
        exec_mode: "fork", 
        instances: "1", 
        wait_ready: true, 
        autorestart: false, 
        max_restarts: 5, 
        interpreter : "python" 
      }] 
}    
  • If we have to change the port number of our django application, then we can change the port no in args attribute of echosystem.config.json file. After done the changes in echosystem.config.json file, stop the server using

  • List item

    pm2 command like this:

    • pm2 stop all // this command will stop all server.
    • pm2 stop 0 // if you know the pm2 id then execute command like this.
    • pm2 start echosystem.config.json // again start the django application.

Download complete sample project from here django_with_pm2

Community
  • 1
  • 1
  • 3
    this is run by using manage.py runserver, how can one create a config that runs gunicorn? – Fed Sep 29 '21 at 13:26
  • @FedericoCapaldo, were you able to make it work with gunicorn? looking to make pm2 work with gunicorn in a virtualenv. The real issue is to set the virtualenv in pm2.. – Alejandro Jul 28 '22 at 12:40
2

This is indeed a very broad question, but I would like to my personal point of view because I use both frameworks in my projects.

express(nodejs) to django(python)

Express and Django are both web frameworks, but Node and Python do not fall in the same category. You could say JS and Python better. Node is a runtime environment for JS, so a tool like pm2 effectively manages Node processes.

For web projects based on Python you need to select a way (interface) to serve them. Common possibilities are CGI and WSGI.

I cannot talk about flask, but Django recommended way is WSGI. There are several options including mod_wsgi, uwsgi and gunicorn. Any tool to control their processes depends on which you choose.

In this sense, this is one level of complexity more. I would recommend you to explore all of them, read about them, possibly test them and pick the one that is best for your needs.

I personally prefer uwsgi because I find it very fast, especially under load, and sometimes mod_wsgi if the host is so configured (doh). UWSGI has an excellent stats solution.

Not to mention that there are many tools for whatever server you end up using with WSGI.

Having said that, will you will not easily reach the response times of node for simple requests. But when in node things get complicated (too many callbacks, too many queries etc) then times get pretty similar. On the other hand, as soon as you get a grasp on Django, you will be amazed on how little time you need to write and maintain code comparing to the callback hell and completely immature data layer of Node.

Wtower
  • 18,848
  • 11
  • 103
  • 80
  • Thank you very much! I'm a bit new in the python world. Some things are very strange to me. In nodejs you could stack a lot of packages and use them in one js file. But in python i have no idea how to mix for example scrapy and django. – ruttydm Jan 06 '17 at 18:54
  • Welcome! What do you mean stack packages? I am not familiar with scrappy, but I find code and package reusability in Python far simpler. Take for example when in node you need to extend some sequelize or mongoose models into reusable modules... – Wtower Jan 06 '17 at 19:17
  • so in brief, what is the `pm2` command-line equivalent for running a django project? – Fed Sep 29 '21 at 13:25
-1

 {
  "apps": [
    {
      "name": "{name}",
      "cwd": "/srv/{name}",
      "args": "runserver",
      "script": "manage.py",
      "exec_mode": "fork",
      "exec_interpreter": "python"
    }
  ]
}
sindhu
  • 17
  • 3