Giter VIP home page Giter VIP logo

django_posix_spawn's Introduction

Purpose

Running long processes from django is hard. There are many service based solutions, but those aren't always wanted.

This lib spawns a new process from a django session. Typically used to run manage.py commands. If you need communication back to django, I use memcached to excahnge data.

posix_spawn() is ideal to use over fork() because it doesn't copy your database/network/file sockets to the new process. Running fork() inside django can cause major issues if you don't clean up correctly.

Usage

I assume your django app name is: website

  1. Inside your django app, create a libs dir: $(BASE)/website/libs

  2. copy django_posix_spawn into libs: $(BASE)/website/libs/django_posix_spawn

  3. Run a command in the background:

     from website.libs.django_posix_spawn import posix_spawn, run_manage
     
     	#same as running: Echo 9
     posix_spawn( '/usr/bin/echo', ['9']) 
     
     	#same as runing: /usr/bin/python manage.py migrate
     run_manage( 'migrate' )
    

Tip of the hat

This code was taken from: https://github.com/projectcalico/python-posix-spawn I made modifications to:

  • Run in python3
  • Prevent zombie child processes
  • Fit into django smoothly
  • Compile C code without needing a custom setup/install script
  • Handle arguments in a user friendly way

django_posix_spawn's People

Contributors

lukedupin avatar

Watchers

 avatar  avatar

django_posix_spawn's Issues

Document how this differs from `os.spawn*`

As far as I can see, this functionality is really similar to os.spawn and relatives. The differences seem to be:

However, in Python 3.4 and later the second part has been mostly fixed by PEP 446 as far as I can see. It looks like stdin/stderr/stdout are not fixed, but 1) these are not so dangerous, and 2) you can fix them pretty easily using nohup or other methods.

Overall, with Python 3.4 I'm using this:

os.spawnlp(os.P_NOWAIT, "nohup", "nohup", "my_program_here")

It would be great if you could add some docs on whether this is a good approach if applicable (e.g. after Python 3.4)

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.