Start the daemon from Django

Source: Internet
Author: User

You want to start a program that has been running in the background through Django.
Whether it is through the use of multi-thread (threading), or multi-process (subprocess, Multiprocessing, OS. fork), the page will return content, but the page remains in the waiting status. the reason is that after a thread is started in Django, Django will think that there is still a message to be returned unless the start thread is killed or the thread ends.

Therefore, create a daemon and interact with Django after it is started. however, it is difficult to separate daemon and Django. it is decided that daemon should be started during Django startup. the method is to place the daemon human process in the settings of Django. in py.

Unexpectedly, Django loaded settings five times during startup. py file, My daemon also started 5 times. in addition, at the beginning, my daemon only interacts with each other through pipe:

P = subprocess. popen (CMD, shell = true, stdin = subprocess. Pipe, stdout = subprocess. Pipe) <br/> In, out = P. stdin, P. stdout

 

 

The subsequent control must be

From settings import in, out <br/> in. Writes (CMD + "/N") <br/> in. Flush ()

As mentioned above, there are three problems.

1. when using pipe for command interaction, it is okay if a starts B and then a interacts with B, but because my program needs to encapsulate a layer, the result will become a start B, B starts C, A and B interact through simple calls, B communicates with C through pipe, and a instructs B to send commands to C. I don't know why, pipe communication is not feasible.

2. Pipe communication has the disadvantage that it can only communicate on the local end, but there will be multiple servers as needed, so pipe communication is actually not feasible.

3. How to prevent multiple daemon instances from being started when Django is started


Solution:

1. Change pipe communication to socket communication, and the subsequent commands will directly interact with each other through socket. In this way, problems 1 and 2 are solved.

S = socket. socket (socket. af_inet, socket. sock_dgram) <br/> S. BIND ("", Port) <br/> while true: <br/> data, ADDR = S. recvfrom (1024) <br/> If data: <br/> Print ("do something ")

2. It is easy to prevent multiple deamon instances from being started. Modify the above Code and check whether the port is occupied to determine whether to continue running.

S = socket. socket (socket. af_inet, socket. sock_dgram) <br/> try: <br/> S. BIND ("", Port) <br/> quit T: <br/> # exit directly when the port is used and no longer start <br/> sys. exit (0) </P> <p> while true: <br/> data, ADDR = S. recvfrom (1024) <br/> If data: <br/> cmd

If you simply create a new thread daemon or daemon, the pseudo daemon disappears when Django exits. in Linux, to become a real daemon, you must follow the steps below (refer to Python cookbook) to ensure that the client can still communicate with the daemon in case of exceptions in DJANGO:

Try: <br/> pid = OS. fork () </P> <p> If pid> 0: # exit the parent process and enter the child process <br/> sys. exit (0) <br/> failed T: <br/> pass <br/> # create a session ID, which is out of control from the current terminal <br/> OS. chdir ('/') <br/> OS. umask (0) <br/> OS. setsid () <br/> # become a real daemon <br/> try: <br/> pid = OS. fork () </P> <p> If pid> 0: # exit the parent process and enter the sub-process. Then it truly becomes daemon. <br/> sys. exit (0) <br/> failed T: <br/> pass <br/> S = socket. socket (socket. af_int, socket. sock_dgram) <br/> try: <br/> S. BIND ("", Port) <br/> Statement t: <br/> sys. exit (0) <br/> while true: <br/> data, ADDR = S. recvfrom (1024) <br/> If data: <br/> cmd

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.