Managing a process from Django

22 views
Skip to first unread message

Kasper Laudrup

unread,
Jan 8, 2018, 11:49:56 AM1/8/18
to Django users
Hi fellow Django users,

I'm working on creating a Django application for managing DHCP leases
and DNS entries and for that I would like to be able to (re)start the
DHCP daemon from Djano.

I think it would be best to simply use a popen object from pythons
subprocess module (most likely wrapped in a class), but I'm not really
sure which place would be the most logical place to keep that object? Of
course there should only be a single instance of that object.

I have also been looking into using some kind of service framework, but
what I have found so far seems to be aimed at task queues (celery etc.),
which would definitely be an overkill for my use case.

It could also be that I'm approaching this the wrong way, in which case
I would be happy to hear better ideas on how to do this.

Thanks a lot for any input.

Kind regards,

Kasper Laudrup

Matemática A3K

unread,
Jan 8, 2018, 11:59:08 AM1/8/18
to django...@googlegroups.com
you can find out which distribution is using, then for each distribution there's a different command for restarting DHCP (systemctl, upstart, system-v) and with the same execute it with sudo to some user with privileges to just do that. You can call that function ("restart_dhcp()") from any django view.

HTH
 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f86f877c-3462-b8bd-95ac-3da1c77c8f72%40stacktrace.dk.
For more options, visit https://groups.google.com/d/optout.

Kasper Laudrup

unread,
Jan 8, 2018, 12:41:35 PM1/8/18
to django...@googlegroups.com
Hi Matemática A3K,

On 2018-01-08 17:58, Matemática A3K wrote:
>
> With this
> https://stackoverflow.com/questions/89228/calling-an-external-command-in-python
> you can find out which distribution is using, then for each distribution
> there's a different command for restarting DHCP (systemctl, upstart,
> system-v) and with the same execute it with sudo to some user with
> privileges to just do that. You can call that function
> ("restart_dhcp()") from any django view.
>

Thanks a lot for your answer.

I would rather avoid using any kind of init system for managing dhcpd,
since I would like to run the Django app in a docker container (running
systemd inside docker is not a good idea) and the options given for eg.
logging and configuration would be quite different from the standard
installation of dhcpd on any distro.

So my question is more related to any kind of process that should be
managed from a Django app.

But thanks a lot for your input.

Kind regards,

Kasper Laudrup

Matemática A3K

unread,
Jan 8, 2018, 1:01:06 PM1/8/18
to django...@googlegroups.com
On Mon, Jan 8, 2018 at 2:41 PM, Kasper Laudrup <lau...@stacktrace.dk> wrote:
Hi Matemática A3K,

On 2018-01-08 17:58, Matemática A3K wrote:

With this https://stackoverflow.com/questions/89228/calling-an-external-command-in-python
you can find out which distribution is using, then for each distribution there's a different command for restarting DHCP (systemctl, upstart, system-v) and with the same execute it with sudo to some user with privileges to just do that. You can call that function ("restart_dhcp()") from any django view.


Thanks a lot for your answer.

I would rather avoid using any kind of init system for managing dhcpd, since I would like to run the Django app in a docker container (running systemd inside docker is not a good idea) and the options given for eg. logging and configuration would be quite different from the standard installation of dhcpd on any distro.

Then it will be easier for you :)
 

So my question is more related to any kind of process that should be managed from a Django app.


Django does not manages processes, Django is a web framework. Python can manage processes. What kind? Any :)
 
But thanks a lot for your input.

Kind regards,

Kasper Laudrup
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Kasper Laudrup

unread,
Jan 8, 2018, 3:01:50 PM1/8/18
to django...@googlegroups.com
Hi again,

>
> So my question is more related to any kind of process that should be
> managed from a Django app.
>
>
> Django does not manages processes, Django is a web framework. Python can
> manage processes. What kind? Any :)
>

I am fully aware of that, thank you :-)

I guess my question is then actually more: if I would like to have one
single instance of an object that I can call safely from models (or
possibly views or whatever) where would be the right place to put that
class and code?

It seems like a fairly basic question, but I haven't found any obvious
solution for that by reading the documentation, so maybe I'm just
missing something basic? I must admit my experience with Django
(although a good one) is fairly limited.

Thanks a lot for your help so far.

Kind regards,

Kasper Laudrup

Antonis Christofides

unread,
Jan 8, 2018, 4:12:10 PM1/8/18
to django...@googlegroups.com
Hello,

When you say "call [an instance of an object]", what exactly do you mean?

Could you tell us more about what this class/object is and why you need to
"call" (access?) it in an unusual way?

Regards,

Antonis

Antonis Christofides
http://djangodeployment.com

Kasper Laudrup

unread,
Jan 8, 2018, 4:43:23 PM1/8/18
to django...@googlegroups.com
Hi Antonis,

On 2018-01-08 22:10, Antonis Christofides wrote:
> Hello,
>
> When you say "call [an instance of an object]", what exactly do you mean?
>

Sorry, I meant an instance of a class or just an object.

> Could you tell us more about what this class/object is and why you need to
> "call" (access?) it in an unusual way?
>

Nothing unusual about the class or object, sorry about the confusion.

My question is probably fairly simple and I'm really sorry if there's
something very basic that I'm missing.

I want to create an instance of some class when the Django application
I'm creating is ready (eg. by connecting to the ready() signal or
similar) and then be able to call methods on that single instance from
my models.

Thanks a lot for the help so far.

Kind regards,

Kasper Laudrup

Antonis Christofides

unread,
Jan 8, 2018, 4:47:31 PM1/8/18
to django...@googlegroups.com
OK, but why do you need this? What functionality is this going to have?

Antonis Christofides
http://djangodeployment.com

Joakim Hove

unread,
Jan 8, 2018, 4:50:35 PM1/8/18
to django...@googlegroups.com
Sounds to me that what you want is a `singleton` implemented in Python . The fact that Django is involved does not seem to be very relevant?


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Kasper Laudrup

unread,
Jan 8, 2018, 5:00:17 PM1/8/18
to django...@googlegroups.com
Hi Antonis,

On 2018-01-08 22:46, Antonis Christofides wrote:
> OK, but why do you need this? What functionality is this going to have?
>

As I wrote in my original question, I'm attempting to write a Django
application for managing DHCP leases and DNS entries.

So, if a user changes an entry by modifying a model in Django, I need to
update some external files and restart a process so the new files will
be reread.

I might be approaching this the wrong way, but I would really like my
Django application to "own" this process, so I would like a global
instance of that which I could start/restart/kill from my Django
application.

I'm perfectly open for other ways to solve this, it just seemed like a
fairly obvious way to do it, but I could very well be wrong.

Thanks a lot and kind regards,

Kasper Laudrup

Kasper Laudrup

unread,
Jan 8, 2018, 5:11:56 PM1/8/18
to django...@googlegroups.com
Hi Joakim,

On 2018-01-08 22:50, Joakim Hove wrote:
> Sounds to me that what you want is a `singleton` implemented in Python .
> The fact that Django is involved does not seem to be very relevant?
>

Indeed, that has crossed my mind and I'm sorry if this is in fact not
very Django related. But I'm wondering if it would be a nice design to
simply have a .py file with a singleton class and then accessing that
from wherever I need it, but thinking about it, then why not?

Sorry if I have created a lot of noise, but that actually seems like
exactly what I want. I might have been trying to overengineer things
instead of just keeping it simple :-)

Thanks a lot for all the help from everyone helping me with my not very
specific questions.

Kind regards,

Kasper Laudrup

Antonis Christofides

unread,
Jan 9, 2018, 1:16:10 AM1/9/18
to django...@googlegroups.com
Ah, OK, sorry I didn't read all the discussion. So I guess that if you keep that
in a global variable, it won't work if your Django app is running in many
processes. (Besides, global variables are rarely a good idea.)

If I understand the problem correctly, what I would do would probably be to
touch a file whenever I make a change and examine the file modification date
each time—if it's more recent than last time I checked, the data has been modified.

Another option is to use the cache. For example, use memcached and store some
data in there. IIRC the cache is shared among all instances of Django.

Regards,

Antonis

Antonis Christofides
http://djangodeployment.com

Kasper Laudrup

unread,
Jan 9, 2018, 3:40:28 AM1/9/18
to django...@googlegroups.com
Hi Antonis,

On 2018-01-09 07:14, Antonis Christofides wrote:
> Ah, OK, sorry I didn't read all the discussion. So I guess that if you keep that
> in a global variable, it won't work if your Django app is running in many
> processes. (Besides, global variables are rarely a good idea.)
>

I agree completely that global variables should be avoided. I find them
to be even more problematic in a language like python where they could
be instatiated by simply importing a file/module. That's probably OK for
a simple value like an integer, but I prefer imports not to have too
many side effects.

> If I understand the problem correctly, what I would do would probably be to
> touch a file whenever I make a change and examine the file modification date
> each time—if it's more recent than last time I checked, the data has been modified.
>
> Another option is to use the cache. For example, use memcached and store some
> data in there. IIRC the cache is shared among all instances of Django.
>

That's not really solving my problem, but thanks for your input. What I
need is a "global" python popen object from the subprocess module.

Someone helpfully suggested using a singleton for that. I really don't
like singletons either, but this could actually be one of the few use
cases where that would make sense, so I think that's what I'll be doing.

Kind regards,

Kasper Laudrup
Reply all
Reply to author
Forward
0 new messages