Suppose you run Django on Linux, and you have a view, and you want this view to return data from the cmd subprocess that works with the file created by the view, for example liko:
def call_subprocess(request): response = HttpResponse() with tempfile.NamedTemporaryFile("W") as f: f.write(request.GET['data'])
Now suppose cmd has a very slow startup time, but a very fast run time, and it does not have a daemon mode. I would like to improve the response time of this point of view.
I would like the whole system to work much faster by running several cmd instances in the workers pool, making them wait for input and have call_process ask one of these processes in the worker pool to process the data.
These are really 2 parts:
Part 1. The function that calls cmd and cmd is waiting for input. This can be done using pipes, i.e.
def _run_subcmd(): p = subprocess.Popen(["cmd", fname], stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = p.communicate() # write 'out' to a tmp file o = open("out.txt", "W") o.write(out) o.close() p.close() exit() def _run_cmd(data): f = tempfile.NamedTemporaryFile("W") pipe = os.mkfifo(f.name) if os.fork() == 0: _run_subcmd(fname) else: f.write(data) r = open("out.txt", "r") out = r.read() # read 'out' from a tmp file return out def call_process(request): response = HttpResponse() out = _run_cmd(request.GET['data']) response.write(out) # would be text/plain... return response
Part 2. Recruitment of workers working in the background who are waiting for data. those. we want to expand the above so that the subprocess already works, for example. when a Django instance or this process_ call is initialized , a collection of these workers is created
WORKER_COUNT = 6 WORKERS = [] class Worker(object): def __init__(index): self.tmp_file = tempfile.NamedTemporaryFile("W")
There should be some initialization of the workers, for example:
def init_workers():
Now, what I have above becomes something like:
def _run_cmd(data): Worker.get_worker()
Now the questions are:
Will this work? (I just printed this at the top of my head in StackOverflow, so I'm sure there are problems, but conceptually, will this work)
What are the problems to search for?
Are there any better alternatives to this? for example Can threads work just as well (it's Debian Lenny Linux)? Are there libraries that handle concurrent worker pool processes like this?
Are there any interactions with Django that I should be aware of?
Thanks for reading! I hope you find this problem interesting as I do.
Brian