简体   繁体   中英

running and stopping a script in subprocess

I am not familiar with subprocesses and I would like to have some help with the following problem. I have 3 apps. Lets say I am running them with command like this:

python manage.py app1
python manage.py app2
python manage.py app2

I want to make a main script to control them like run_app1 or stop_app1

Everything runs in linux.

My apologies for my poor explanation. I have a problem called Dyslexia, also known as reading disorder. It is some times hard for me to write down what I am thinking. Thank you for reading or any help

Using the subprocess Python module a first step could be something like this:

# Master
from multiprocessing.connection import Listener
from subprocess import Popen, PIPE
import socket
import sys

port = 10000
lstn = Listener(('localhost', int(port)), authkey=b'secret')
proc = Popen((sys.executable, 'worker.py', str(port)), stdout=PIPE, stderr=PIPE)

conn = lstn.accept()
conn.send([1, 'Brian', None])
print(proc.stdout.readline())
# Worker
from multiprocessing.connection import Client
import sys

port = int(sys.argv[1])
conn = Client(('localhost', port), authkey=b'secret')

while True:
    try:
        msg = conn.recv()
        print('Received: %s', str(msg))
        sys.stdout.flush()
    except EOFError:
        break

The master process initializes a listener and then opens the worker process. Messages can be sent to the worker via the connection object and stdout and stderr go back to the master process.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM